2013-10-07 47 views
0

對不起,如果這是一個愚蠢的問題,但我正在尋找一種方式來中斷並開始循環的另一個迭代,如果用戶的輸入爲空。 這裏是我的代碼:Java:如果變量爲空(來自用戶輸入);打破;否則繼續主要方法

while(true) { 
    // Get the users input 
    Scanner Input = new Scanner(System.in); 
    System.out.println("Enter text:"); 
    String studentInfo = Input.nextLine(); 

    if (studentInfo == null) { 
     System.out.println("Please enter valid infomation."); 
     break; 
    } 
    continue; 
} 

中場休息後,我希望它回去,而(真),並從那裏重新開始,再次要求輸入用戶。

謝謝你們,

JT

+4

請注意'nextLine()'永遠不會返回'null'。 –

+1

爲什麼你需要'休息'比? – Admit

+0

唉好吧,我不知道,我只是試圖阻止我的代碼崩潰時,用戶剛剛進入沒有實際的輸入,謝謝你們! – jtaylor94

回答

3

檢查:

if (studentInfo.isEmpty()) { 
    continue; 
} 

while循環內刪除Scanner input = new Scanner(System.in);聲明。

相關問題