2015-04-12 68 views
2

我有一個java代碼,在那裏我想從用戶那裏得到一個整數。如果這個輸入是一個整數,那麼它的精確控制將會經過下一個,但是如果輸入不是整數,控制將再次進行掃描。問題是除了一個整數是由用戶給出while循環一遍又一遍地循環。怎麼解決?請幫助我。使用掃描儀時清除輸入緩衝區

我已經嘗試了在這個網站的問題的答案,但沒有得到任何結果。

這裏是我的代碼 -

import java.io.* ; 
import java.util.Scanner; 

class A{ 
    int name; 
    int take_int(){ 

     Scanner data=new Scanner(System.in); 
     //BufferedWriter BW=new BufferedWriter(); 


    while(true){ 
     int fault_res=0 ; 
     try{ 
      System.out.print("Enter A Number : "); 
      //BW.flush();   
      name=data.nextInt(); // comment : if user give an alphabet 
           // then the control rounds again and 
           // again, how to solve? System is not 
           // taking any input after entering 
           // any alphabet first time, but it 
           // should take as per my code, I 
           // think is is happening due to i/p 
           // buffer, unable to delete it. 

     } 
     catch(Exception E){ 
      System.out.println("\nSomething Went Wrong !!\nRetry Please !!!"); 
      fault_res=1 ; 
     } 
     if(fault_res==0){break ;} 
    } 
    System.out.println("\nnumber at other class "+name); 
    return name ; 
    } 
} 
class sn{ 
    public static void main(String args[]){ 
     A obA=new A(); 
     int number=obA.take_int(); 
     System.out.println("\nYou have entered "+number); 
    } 
} 

回答

1

您可以從catch塊清除輸入。如果你不想要除整數以外的任何東西,那麼只需在catch塊中執行inputStream.nextLine();

這裏是一個只接受整數輸入的通用代碼片段。

private static int takeOnlyIntegerInput() { 
     Scanner keyboard = new Scanner(System.in); 
     boolean isValid = false; 
     int num=-1; 
     while (isValid == false) { 
      System.out.print("Please enter an: "); 
      try { 
       num = keyboard.nextInt(); 
       isValid = true; 
      } catch (InputMismatchException ex) { 
       System.out.println("Wrong input. Ony integer input will be processed."); 
       //clean up the garbage input 
       keyboard.nextLine(); 
      }finally{ 
       keyboard.close(); 
      } 
     } 
     return num; 
    } 
+0

什麼是keyboard.close()的工作? –

+0

它關閉流。做'keyboard.close'是「好習慣」的一部分。它不是必需的(對於程序的正確性),但它可以避免內存泄漏。 – Nirmal

0

您應該添加,在異常的catch條款,無論是data.next()data.nextLine()

他們選擇哪一種取決於輸入的類型和您期望的錯誤類型。如果你希望在同一行更多的投入,例如:

XXXX 1234 
5678

那麼只有data.next()將正常工作,因爲它會跳過XXXX和下一個標記將1234這是你想要的。

如果在這種情況下使用data.nextLine(),那麼這兩個XXXX1234會被跳過 - 一切,直到行的結束,要讀取的下一個標記將5678

+0

是的。但是我想通過清除輸入緩衝存儲器來實現,在這種存儲器中,先前輸入的字母或其他字符(不是整數)將被刷新,緩衝存儲器中將沒有任何內容用於在進一步掃描時進行拾取。 –

+0

那麼,你就是這麼做的 - 調用data.next()或data.nextLine(),而不用做任何事情。 'data.next()'將清除所有內容直到下一個分隔符,'data.nextLine()'將清除所有內容直到行尾。 – RealSkeptic

0

重寫你的程序如下,它會工作。

import java.io.* ; 
import java.util.Scanner; 

class A{ 
    int name; 
    int take_int(){ 

    Scanner data=new Scanner(System.in); 

    while(true){ 
     int fault_res=0 ; 
     try{ 
      System.out.print("Enter A Number : "); 
      String input = data.nextLine();   
      name=Integer.parseInt(input); 

     } 
     catch(Exception E){ 
      System.out.println("Something Went Wrong !! Retry Please !!!"); 
      fault_res=1 ; 
     } 
     if(fault_res==0){break ;} 
    } 
    System.out.println("\nnumber at other class "+name); 
    return name ; 
    } 
} 
class sn{ 
    public static void main(String args[]){ 
     A obA=new A(); 
     int number=obA.take_int(); 
     System.out.println("\nYou have entered "+number); 
    } 
}