簡體   English   中英

字符串不從掃描器類獲取輸入

[英]String is not taking input from scanner class

在while循環結束時,我使用scanner類將一個字符串作為用戶的輸入,但它沒有接受任何輸入。

我已經導入了Scanner類,但無法弄清楚為什么它不等待接受任何輸入。

請指導我。

 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;
        }

   }


   }
  }

因為你正在使用nextInt()所以它不會消​​耗換行符,所以稍后當你執行nextLine時,它將消耗該空行。

所以你的問題的解決方案是在你的最后一個nextInt()之后放下nextLine()

s.nextInt()之后添加以下行

s.nextLine();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM