Question

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;
    }
}
Was it helpful?

Solution

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)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top