I'm currently writing a program for a interface for a college program. I have switch (str)
which reads the users input and decides which function to handle. One of my cases for my switch is to delete a student from the ArrayList
college consisting of students. Whenever I call this function it would print
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at CollegeTester.main(CollegeTester.java:40)
line 40 being str = myInput.nextLine();
I have tested the other cases and they seems to loop fine, but whenever I invoke the delete case it would give me this error. Could I get some insight on what's going on and possibly a fix?
do
{
Scanner myInput = new Scanner(System.in);
System.out.println("College directory commands: ");
System.out.println("add - Add a new Student ");
System.out.println("find - Find a Student ");
System.out.println("addQuiz - Add a quiz score for a student ");
System.out.println("findHighest - Find a student with the highest quiz score ");
System.out.println("delete - Delete a Student ");
System.out.println("getAverage - the average quiz score of the school.");
System.out.println("quit - Quit");
System.out.print("Enter a command: ");
//Scanner myInput = new Scanner(System.in);
str = myInput.nextLine();
switch(str)
{...
case "delete":
{
System.out.print("Enter a Student's number: ");
x = myInput.nextLine();
System.out.println(str);
langara.deleteStudent(x);
System.out.println(str);
}break;
case "getAverage":
{
System.out.println("The average quiz score for this college is: "+langara.getAverage());
}break;
} while(!"quit".equals(str));
public void deleteStudent(String studentID)
{
boolean t = false;
Scanner theInput = new Scanner(System.in);
String tmp;
for(student x: college)
{
if(x.getStudentID().equals(studentID))
{
//prompt user for a "yes" confirmation before deletion
System.out.print("Deleting Student: "+findStudent(studentID).getName()+" Please confirm witha \"yes\": ");
tmp = theInput.nextLine();
if(tmp.equals("yes"))
{
System.out.println("Student with ID "+x.getStudentID()+" is removed.");
college.remove(x);
t=true;
break;
}
else
{
break;
}
}
}
if(t==false)
{
System.out.println("No such student with ID "+studentID+" exsists at this college.");
}
theInput.close();
}