0

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();
}
fabian
  • 80,457
  • 12
  • 86
  • 114
j doe
  • 83
  • 1
  • 7
  • Were is `str` defined ? (Is all this long code and explanation essential to demonstrate the problem ? Please post an [MCVE] ) – c0der Sep 26 '16 at 09:34
  • i guess the problem with langara.deleteStudent(x); , can you edit the answer and share the code of ur deleteStudent – Amer Qarabsa Sep 26 '16 at 09:34
  • @c0der i defined str outside the do loop – j doe Sep 26 '16 at 09:36
  • @Amer Qarabsa i added the deleteStudent at the end – j doe Sep 26 '16 at 09:37
  • on a side note i get warnings for resource leak if i don't close the scanner so in this case where i may need to access deleteStudent() many times where would i insert the close command. – j doe Sep 26 '16 at 09:48

0 Answers0