簡體   English   中英

Java,使用 Scanner 嘗試捕獲

[英]Java, try-catch with Scanner

我正在創建一個小算法,這是其中的一部分。

如果用戶輸入非整數值,我想輸出一條消息,讓用戶再次輸入一個數字:

boolean wenttocatch;

do 
{
    try 
    {
        wenttocatch = false;
        number_of_rigons = sc.nextInt(); // sc is an object of scanner class 
    } 
    catch (Exception e) 
    {
        wenttocatch=true;
        System.out.println("xx");
    }
} while (wenttocatch==true);

我得到了一個永無止境的循環,我不知道為什么。

如何識別用戶是否輸入了一些非整數?
如果用戶輸入了一個非整數,如何讓用戶重新輸入?

更新
當我打印異常時,我得到“InputMismatchException”,我該怎么辦?

在讀取項目之前, Scanner不會前進。 這在Scanner JavaDoc中提到。 因此,您可以使用.next()方法讀取值,或者在讀取int值之前檢查是否有hasInt()

boolean wenttocatch;
int number_of_rigons = 0;
Scanner sc = new Scanner(System.in);

do {
    try {
        wenttocatch = false;
        number_of_rigons = sc.nextInt(); // sc is an object of scanner class
    } catch (InputMismatchException e) {
        sc.next();
        wenttocatch = true;
        System.out.println("xx");
    }
} while (wenttocatch == true);

你不必嘗試捕捉。 此代碼將為您解決問題:

public static void main(String[] args) {
    boolean wenttocatch = false;
    Scanner scan = new Scanner(System.in);
    int number_of_rigons = 0;
    do{
        System.out.print("Enter a number : ");
        if(scan.hasNextInt()){
            number_of_rigons = scan.nextInt();
            wenttocatch = true;
        }else{
            scan.nextLine();
            System.out.println("Enter a valid Integer value");
        }
    }while(!wenttocatch);
}

任何時候遇到異常, wenttocatch都會設置為true ,程序就會陷入無限循環。 只要你沒有得到異常,你就不會得到無限循環。

如果 sc.nextInt() 導致錯誤的邏輯是這樣的

1) gotocatch 設置為 false

2) sc.nextInt() 拋出錯誤

3) gotocatch 設置為 true

4) 重復[因為 gotocatch 是真的]

在 catch 語句中解決這個 set goocatch=false

catch (Exception e) {
               wenttocatch=false;
               System.out.println("xx");
            }

如果您做的比這里顯示的多,請使用計數器[如果您的計數或布爾值,如果您不是],但除非您做的更多,否則請執行上面的第一件事

boolean wenttocatch;
int count = 0;

            do{


             try {
                 wenttocatch=false;
                number_of_rigons=sc.nextInt(); // sc is an object of scanner class 
            } catch (Exception e) {
               count++;
               wenttocatch=true;
               System.out.println("xx");

            }

            }while(wenttocatch==true && count < 1);

回復評論:

我認為您想獲得整數,直到用戶不再輸入。 根據您的輸入,一種方法是這樣

int number_of_rigons;
while((number_of_rigons = sc.nextInt()) != null){
    //do something
}

您只需使用 hasNext(); java中的方法,而不是try and catch。 hasNext() 是 Java Scanner 類的一個方法,如果此掃描器的輸入中有另一個標記,則返回 true。

 String[] stringArray = new String[lines];
   int i = 0;
   try (Scanner sc = new Scanner(file)) {
       while (sc.hasNextLine()) {
           String data=sc.nextLine();
           stringArray[i] = data;

           i++;
       }
   } catch (FileNotFoundException fileNotFoundException) {
       System.err.println("Error opening file.");
       throw new FileNotFoundException();
   } catch (NoSuchElementException e) {
       System.err.println("Error in file record structure");
       throw new NoSuchElementException();
   } catch (Exception e) {
       e.printStackTrace();
   }
  

/*在里面添加了掃描儀聲明嘗試我新的編程它對我有用,但我不知道它是否是好的做法*/:

    boolean wenttocatch;
    do 
    {
        try 
        {
           
        Scanner sc=new Scanner(System.in);
         wenttocoatch = false;
        number_of_rigons = sc.nextInt(); 
    } 
    catch (Exception e) 
    {
        wenttocatch=true;
        System.out.println("xx");
    }
} while (wenttocatch==true);
....
try {
  ....
} catch (Exception e) {
  sc.next();
  wenttocatch=true;
  System.out.println("xx");
}

暫無
暫無

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

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