2015-10-15 117 views
1

我想製作一個不能爲空的掃描器。因此,當掃描儀中沒有任何內容時,如果要按Enter鍵,我想發出警告消息。我有這段代碼。空Java掃描器

if (user_input.hasNext())// only if scanner is empty and enter is pressed{ 
     System.out.println("Scanner can not be empty"); 
    } 
+0

閱讀整條生產線。如果裏面什麼都沒有,打印你的警告。 – azurefrog

+0

你認爲,如果他被告知這個錯誤信息,那麼這個「不在街上的人」會認爲什麼? - 也許「請在按下」輸入「鍵之前輸入數據」就足夠了。 – laune

回答

4
Scanner sc=new Scanner(System.in); 
    String input = sc.nextLine(); 
    if(input.isEmpty()) 
    { 
     System.out.println("Scanner can not be empty"); 
    }