0

In the end of while loop, I am using scanner class to take a string as an input from the user but it is not taking any input.

I have already imported the Scanner class but not able to figure out why it is not waiting to take any input.

please guide me.

 import java.util.Scanner;

/**
*
* @author Student
*/
public class Exception {


public static void main(String[] args) {
   Scanner s = new Scanner(System.in);


   /*

            Creating a UserDefined Operations `   

   */


   while(1==1){

        System.out.println("\nEnter one of the following operations: ");
        System.out.println("1. Arithmeric Exception");
        System.out.println("2. ArrayIndexOutOfBounds Exception");
        System.out.println("3. NumberFormat Exception");
        System.out.println("4. Exit");

        int choice=s.nextInt();

        switch(choice)
        {
            case 1: 

                 //ArithmeticException 
                System.out.println("Enter the numerator: ");
                int num=s.nextInt();
                System.out.println("Enter the denomerator: ");
                try{
                    int dem=s.nextInt();
                    int divide=num/dem;
                }
                catch(ArithmeticException e){e.printStackTrace();}
                break;

            case 2:
                 //ArrayIndexOutOfBoundException 
                 System.out.println("Enter the size of array");
                 int size=s.nextInt();
                 int[] array = new int[size];
                 System.out.println("Enter the elements: ");
                 for(int x:array)
                     x=s.nextInt();
                 System.out.println("Enter the index of array to be accessed:");
                 try{
                      int index=s.nextInt();
                      System.out.println("The array to be accessed is: "+array[index]);
                 }catch(ArrayIndexOutOfBoundsException e){e.printStackTrace();}
                 break;


            case 3:
                     //NumberFormatException
                     System.out.println("Enter a number");
                     String s1=s.nextLine();
                     try{
                         Integer.parseInt(s1);
                     }
                     catch(NumberFormatException e){e.printStackTrace();}
                     break;
            case 4:
                    System.exit(0);
            default:
                    System.out.println("Invalid choice");

        }

        System.out.println("Do you want to continue:\nyes/no ");
        String str;
                str=s.nextLine();
        if(str.equals("no")||str.equals("No")||str.equals("n"))
            break;
        else
        {
            System.out.println("Invalid Input . Existing The Program");
            break;
        }

   }


   }
  }

2 Answers2

0

because you are using nextInt() so it will not consume newline so later when you do nextLine after that ,it will consume that empty line.

so solution to your problem is put nextLine() after your last nextInt()

user1627167
  • 339
  • 1
  • 8
0

Add below line after s.nextInt()

s.nextLine();
Vamsi
  • 780
  • 1
  • 6
  • 24