문제

I'm still having an issue with the line String parts = input.nextLine();. It keeps crashing and if i remove "Line", it is fine but i want to read the whole String including spaces and store it. Assuming that I have declared all the variables and array previously.

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;
    }
}
도움이 되었습니까?

해결책

Look at nextLine:

Throws:

NoSuchElementException - if no line was found

IllegalStateException - if this scanner is closed

Make sure the scanner is not closed before you reach the line that causes the problem.. also make sure that.. there is a line.

I don't see any other problem that could cause your program to "crash" (You'll help us to help you if you better define what "crash" is)

다른 팁

You can use the BufferedReader :

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

or the scanner:

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

both should work without any problems.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top