所以它已經有一段時間,因爲我真的寫了任何基於java的控制檯,Java掃描程序對象跳過行?
我有這個方法,下面,應該問我每一行,對嗎?相反,我得到一個提示,然後三個..我在這裏做錯了什麼?
try {
System.out.println("Please enter the product's LONG details: ");
product.setLongDetails(sc.next()); //set products long details
System.out.println("Please enter the product's SHORT details: ");
product.setShortDetails(sc.next()); //set short details
System.out.println("Please enter the product's UPC data: ");
product.setUpc(sc.next()); //set upc
System.out.println("Please enter the product's stock: ");
product.setStock(sc.nextInt());
System.out.println("Please enter the products price. ");
System.out.println("This MUST be entered with no dollar sign.");
product.setPrice(sc.nextBigDecimal());
InventoryManager.addProduct(product); //add product to database
}
我的控制檯輸出是這樣的:
Please enter the product's LONG details:
Fireplace Cleaning Package
Please enter the product's SHORT details:
Please enter the product's UPC data:
Please enter the product's stock:
BUILD STOPPED (total time: 3 minutes 5 seconds)
掃描儀剛剛類聲明中初始化:
public static Scanner sc = new Scanner(System.in); //class level scanner object for reuse.
如果我每個對象之間的調用sc.nextLine()設置方法,它會提示正確,但會得到錯誤信息:
Please enter the product's SHORT details:
Fire Poker
個
顯示爲對象: 請輸入您的選擇: Fire
你是如何初始化掃描儀''sc''? – f1sh
它被聲明爲類變量。我將添加初始化到問題,給我一秒鐘。另外,我只是在每次調用之間使用sc.nextLine()進行測試,並且可能已經修復了它,但是我沒有完全確定沒有檢查數據文件 –