So my homework assigment was to create a new method in the class section called, public void deleteStudentsWithName(String)...So what it does is reads a files, converts it into an array, and then it asks the user which name you want to delete. My program is very messy(in my opinion), but somehow it works until it runs into an infinite loop by having two arrays with the same string and one is at the top and one at the end. The program just stops!!! Please help

Ex:


Enter name of file containing Student info: student1.txt

In forward order, students are:
Hal (10)
John (10)
John (10)
Ryan (10)
Bill (10)
Ryan (10)

Enter name of a Student to delete: John

Deleted 2 students with name "John"

In forward order, students are:
Hal (10)
Ryan (10)
Bill (10)
Ryan (10)

Enter name of a Student to delete: Hal

Deleted 1 students with name "Hal"

In forward order, students are:
Ryan (10)
Bill (10)
Ryan (10)

Enter name of a Student to delete: Ryan\\here is where it runs into an infinite loop. it just wont continue

import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw10
{
//-----------------------------------------------------------------------
   public static void main (String [] args) throws Exception
   {
      Scanner kb = new Scanner(System.in);

      System.out.print("\nEnter name of file containing Student info: ");
      String filename = kb.nextLine();

      Section sec = new Section(filename);

      System.out.println("\nIn forward order, students are:");
      sec.print(System.out);

      while ( sec.howMany() != 0 )
      {
         System.out.print("\nEnter name of a Student to delete: ");
         String badName = kb.nextLine();
         int howManyBefore = sec.howMany();
         sec.deleteStudentsWithName(badName);
         int howManyAfter = sec.howMany();
         System.out.println("\nDeleted " + 
                            ( howManyBefore - howManyAfter ) +
                            " students with name \"" +
                            badName +
                            "\"");

         System.out.println("\nIn forward order, students are:");
         sec.print(System.out);
      }

      System.out.println("\nThe Section is now empty.");
  }
//-----------------------------------------------------------------------
} // end class Hw10
/////////////////////////////////////////////////////////////////////////
class Section
{
   private Student [] a;
   private int used;
   private final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
   public int howMany() { return used; }
//-----------------------------------------------------------------------
   public Section ( String filename ) throws Exception
   {
      Scanner sc = new Scanner(new File(filename));

      a = new Student[INIT_SIZE];
      used = 0;

      while ( sc.hasNext() )
      {
         Student s = Student.read(null,sc);

         if ( used == a.length )
         {
            Student[] newA = new Student[2*a.length+1];
            for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
            newA[used] = s;
            used++;
            a = newA;
         }
         else
         {
            a[used] = s;
            used++;
         }
      }
   }
//-----------------------------------------------------------------------
	public void deleteStudentsWithName(String s)
	{
		int count =0;
		for (int i = 0; i<=used-1; i++)
		{
			String z = a[i].getName();
			if(z.equals(s))
			{
				if(i==(used-1)){count++;}//my problem might be here
				else
				{
					count++;
					for(int j= i;j<=used-2;j++)
					{
						a[j]=a[j+1]; 
					}
					i-=1;
				}
			}
		}
		used-=count;
	}	
//-----------------------------------------------------------------------
  public void print ( PrintStream ps )
   {
      for ( int i = 0 ; i < used ; i++ )
      {
         ps.println(a[i]);
      }
   }
//-----------------------------------------------------------------------
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
   private String name;
   private int grade;
//-----------------------------------------------------------------------
   public Student ( String name, int grade )
   {
      this.name = name;
      this.grade = grade;
   }
//-----------------------------------------------------------------------
   public String toString ()
   {
      return name + " (" + grade + ")";
   }
//-----------------------------------------------------------------------
   public String getName() { return name; }
//-----------------------------------------------------------------------
   public int getGrade() { return grade; }
//-----------------------------------------------------------------------
   public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
   public static Student read ( PrintStream ps, Scanner sc ) 
   {
      if ( ps != null ) ps.println("Reading a Student record ...");
      if ( ps != null ) ps.print("Enter the name: ");
      String name = sc.nextLine();
      if ( ps != null ) ps.print("Enter the grade: ");
      int grade = sc.nextInt(); sc.nextLine();
      return new Student(name,grade);
   }
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////

Edited 5 Years Ago by Gsterminator: n/a

You should verify that you are indeed in an infinite loop and find out where exactly. Make sure it's not a program crash or something. Putting some println statements in the loops should do the job. If they print forever, you're in an infinite loop.

Line 79 is going to be a serious problem, I think. Suppose used is 3 and I am deleting indexes 0 and 2, as with your last example.

Index 0 is to be deleted. students 1 and 2 become 0 and 1. So far so good. i is decremented to -1. So far so good since we want i to be 0 next trip through the loop. Now we're at the top of the loop again. i is 0(good), count is 1(good), students that were earlier students 1 and 2 are now 0 and 1(good). used is 3. BAD!

There are now only 2 students left. Yet your loop control is thinking there are 3. You're going to have too many trips through that loop and you are going to find "student 2" again and "delete" it, even though you moved it down to student 1. You never adjusted the "used" variable and you never set the student at index 2 null, so "count" is going to be too big. Get rid of the count variable. Make "used" accurate as you go since loop control depends on it. You eventually change "used" but you do it too late.

WOW!!! i figured it out. It was really simple. I really over complicated my code. But thanks for your help

public void deleteStudentsWithName(String s)
	{
		int count =0;
		for (int i = 0; i<=used-1; i++)
		{
			String z = a[i].getName();
			if(z.equals(s))
			{
				used--;
				for(int m =i; m<=used-1;m++)
				{
					a[m]= a[m+1];
				}
				i-=1;
			}
		}
	}
This question has already been answered. Start a new discussion instead.