2013-04-08 110 views
0

我仍然有一個問題,行String部分= input.nextLine();.它不斷崩潰,如果我刪除「線」,它是好的,但我想讀整個字符串包括空格和存儲它。假設我已經聲明瞭所有的變量和數組。String.nextLine()Java - 崩潰

System.out.print("Enter registration number of vehicle: "); 
String inputREGO = input.nextLine(); 
System.out.print(inputREGO); 
boolean flag = false; 
for(int i=0; i<6; i++){ 
    if(inputREGO.equalsIgnoreCase(services[i].getregoNUMBER())){ 
     System.out.print("Enter Part Description: "); 

     String parts = input.next(); 
     System.out.print("Enter Part Cost: "); 

     Double cost = input.nextDouble(); 
     services[i].addPARTDETAILS(parts, cost); 
     flag = true; 
    } 
} 

if(!flag) 
    System.out.println("No registration number were found in the system."); 

public boolean addPARTDETAILS(String partDESCRIPTION, double partCOST){ 
    if(partDESCRIPTION.isEmpty() || partCOST <= 0){ 
     System.out.println("Invalid input, please try again!"); 
     return false; 
    } 
    else{ 
     StringBuffer strBuf = new StringBuffer(); 
     strBuf.append(" ").append(partDESCRIPTION).append(" ($").append(partCOST).append(")"); 
     partLIST += strBuf; 
     System.out.printf("\n%10s", partLIST); 
     System.out.println("\n Parts recorded successfully for vehicle " + getregoNUMBER()); 
     totalPART+=partCOST; 
     return true; 
    } 
} 
+2

什麼是輸入宣佈爲? – 2013-04-08 12:55:42

+1

什麼類型有「輸入」? – dontcare 2013-04-08 12:56:23

+1

請發佈堆棧跟蹤,顯示正在引發異常和在哪裏。我也建議你閱讀[FAQ](http://stackoverflow.com/faq)和這些[提示](http://tinyurl.com/so-hints)。 – 2013-04-08 12:56:25

回答

0

nextLine

拋出:

NoSuchElementException異常 - 如果沒有行被發現

IllegalStateException異常 - 如果此掃描器已關閉

確保s canner是未關閉之前,你到達導致問題的線..也請確保.. 有一條線

我不認爲這可能會導致程序任何其他問題「崩潰」(你會幫助我們來幫助你,如果你更好地定義「撞車」是什麼)

0

可以使用BufferedReader

BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in)); 
String text = bufferRead.readLine(); 

scanner

Scanner scanIn = new Scanner(System.in); 
    String text = scanIn.nextLine(); 

都應該沒有任何問題的工作。