1

I want to know and understand why the program terminates at case 1 and I can't enter an input, even though it doesn't have any error. There's also a warning that says "Resource leak: 'sc' is never closed", I tried closing it but the output is still the same. Thank you in advance!

import java.util.Scanner;

public class method 
{

    public static void main(String[] args) 
    {
         Scanner sc = new Scanner(System.in);
            
         System.out.println("Student Record Program\n"
         + "[1]Add Student\n[2]Display Student Record\n"
         + "[3]Search a Record\n[4]Sort Student Record");

         int choice;
         System.out.print("Enter your choice: ");
         choice = sc.nextInt();
         
         switch(choice)
         {
            //This is the part where I the program terminates and I can't enter any input
            case 1:
                String answer;
                System.out.print("Add Student (Yes/No): ");
                answer = sc.nextLine();
                    if(answer.equalsIgnoreCase("Yes"))
                    {
                        studentsInfo.addStudent();
                    }
                break;
            case 2:
                System.out.print("Display Student Record");
                break; 
            case 3:
                System.out.print("Searching");
                break;
            case 4:
                System.out.print("Sorting");
                break;
            default:
                System.out.print("You've entered an invalid option");
         }
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Silly Bean
  • 31
  • 2

0 Answers0