0

我仍然对 String parts = input.nextLine(); 这一行有疑问。它不断崩溃,如果我删除“Line”,那很好,但我想读取包括空格在内的整个字符串并存储它。假设我之前已经声明了所有变量和数组。

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;
    }
}
4

2 回答 2

0

您可以使用BufferedReader

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

扫描仪

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

两者都应该没有任何问题。

于 2013-04-08T13:00:14.303 回答
0

看看nextLine

抛出:

NoSuchElementException -如果没有找到行

IllegalStateException -如果此扫描仪已关闭

确保在到达导致问题的线路之前未关闭扫描仪.. 还要确保..有一条线路

我没有看到任何其他可能导致您的程序“崩溃”的问题(如果您更好地定义“崩溃”是什么,您将帮助我们帮助您)

于 2013-04-08T13:03:13.527 回答