-2

I am having some issue while using scanner class for some menu of choices from keyboard I am trying to execute below logic from main method

StudentBO country = new StudentBO();
Scanner sc = new Scanner(System.in);
int selection;
System.out.println("Menu : ");
System.out.println("Type any number between 1 and 6");
System.out.println("1)Create a new student");
System.out.println("2)details specific student");
System.out.println("3)details of all students");
System.out.println("4)details of the student age details");
System.out.println("5)details of the student personal info");
System.out.println("6)Exit");
selection = sc.nextInt();
switch (selection)
{
    case 1 :
        System.out.println("Enter studentName ");
        sc = new Scanner(System.in);
        String studentName= sc.nextLine();
        Student studentInfo = student.createStudent(studentName);
        System.out.println("Do you want to continue? Type Yes / No");
        Scanner sc1 = new Scanner(System.in);

    case 2 :
        break;
    case 3 : 
        break;
    case 4 :      
        break;
    case 5 :
        break;
    case 6 :
        break;
} 

When i am selecting choice 1, student is created, and then i have to enter Yes or No, if user selected yes, again user to be prompted from menu of choices (1,2,3,4,5 or 6), if user selected No, then break.

issues i am facing

  1. How to go back to select menu of choices after execution of one case. I want to go back to prompt user with menu of choices after case 1

  2. How to switch from one case to another. in case 1, based on some if condition, I want to call case 2 directly. is that possible?

James Z
  • 11,838
  • 10
  • 25
  • 41
user2883028
  • 163
  • 5
  • 18
  • seems like you're looking for some type of loop. – Ousmane D. Nov 11 '17 at 18:59
  • [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?](https://stackoverflow.com/q/13102045) – Pshemo Nov 11 '17 at 18:59
  • User is prompted to enter Yes or No , say if entered Yes, then again user has to get prompted to enter some choice1 ,2,3,4,5 or 6 , but after entering Yes, control going to case2 , and not prompting to enter 1 ,2,3,4,5 or 6 – user2883028 Nov 11 '17 at 19:09

1 Answers1

0

You can use while loop with boolean for exit while reading the 2nd input.

    StudentBO country = new StudentBO();
Scanner sc = new Scanner(System.in);
int selection;
boolean exit = false;
while (!exit) 
{
System.out.println("Menu : ");
System.out.println("Type any number between 1 and 6");
System.out.println("1)Create a new student");
System.out.println("2)details specific student");
System.out.println("3)details of all students");
System.out.println("4)details of the student age details");
System.out.println("5)details of the student personal info");
System.out.println("6)Exit");
selection = sc.nextInt();
switch (selection)
        {
        case 1 :
            System.out.println("Enter studentName ");
            sc = new Scanner(System.in);
            String studentName= sc.nextLine();
            Student studentInfo = student.createStudent(studentName);
            System.out.println("Do you want to continue? Type Yes / No");
            Scanner sc1 = new Scanner(System.in);
            if (sc1.next().equalsIgnoreCase("no"))
                    exit = true;
                break;

            case 2 :

                break;
            case 3 :

                break;
            case 4 :

                break;
            case 5 :
                break;
            case 6 :
                break;
          }   
}
Melron
  • 99
  • 5
  • This worked , but all the menu choices are shown only for the first time when java program is run , from next prompts , user is asked only to enter the choice like 1,2,3,4,5 or 6 ,and not showing all the menu choices again – user2883028 Nov 11 '17 at 19:27
  • then move the while loop after the last print (exit message) – Melron Nov 11 '17 at 19:31