2016-08-31 82 views
0

我正在通過需要記錄「加侖2%牛奶」之類的掃描儀進行用戶輸入。 現在我有掃描儀記錄字符串與空格和數字?

System.out.print("Enter product description: "); 
    String description = in.next(); 

現在這個工作得很好,比如像「牛奶」,但是當我輸入「加侖2%的牛奶」,它返回一個錯誤。

也,我已經使用

String description = in.nextLine(); 

嘗試,但上打印「進入產品說明:輸入產品的數量:」

我假設是因爲在我的下一行代碼是

System.out.print("Enter product quantity: "); 

在此先感謝您的幫助!

+0

在System.out.print(「輸入產品說明:」)之前有什麼?'如果在此之前按ENTER鍵,那麼該按鍵也需要消耗使用'nextLine' –

+0

請確認輸入產品數量:'在您輸入產品描述之前正在打印? –

+0

idk如何喜歡這個,但這是罪魁禍首!謝謝@ScaryWombat – sparkles

回答

0

如果在System.out.print("Enter product description: ");之前有提示,並且在此之前按ENTER鍵,那麼該按鍵也將需要消耗在nextLine中。

-1

您應該使用in.nextLine()來接收字符串,包括空格和回車鍵。你在一條線上獲得兩個輸出的原因是因爲你正在使用System.out.print(String)方法。這不會將新行添加到輸出流,因此後續對它的調用將追加到同一行。改爲使用System.out.println(String)