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
/////////////////////////////////////////////////////////////////////////