nathan harwood
nathan harwood

Reputation: 1

My Do-While loop breaks when I hit my keyboard input for the second time

I am only a few weeks in on coding so I'm really new. This is a simple program that assists the user in customizing a new car. The program uses different methods for each of the respective menus. each method that isn't main asks the user which of 2 custom car options they would like then the method sends back a cost to the main. Each method that isn't main uses the same format as the method engineCost. I can't figure out why my keyboard input only functions during the first iteration of my do while loop. This is the error I am prompted with on eclipse:

Exception in thread "main" java.lang.IllegalStateException

I did some looking on stack overflow and I don't think I'm knowledgeable enough to search for the answer properly. Any help would be much appreciated.

import java.util.Scanner;

public class Ch5HW {
    public static void main(String[] args){
        double baseCar = 14000;
        double addCost = 0;
        double ttlCost = 0;
        int userOpt;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Basic Car includes: Silver color, 4 cilinder engine, Cooper Tires; cost: $14,000");
        System.out.println("");
        //this do-while loops until the user enters 4 to exit 
        do{
            System.out.println("Build Car Quote");
            System.out.println("1. Engine");
            System.out.println("2. Color");
            System.out.println("3. Tires");
            System.out.println("4. Exit");

            System.out.print("Enter Option: ");
            userOpt = 0;
            // this keyboard input only works on the first iteration of the loop
            userOpt = keyboard.nextInt();
            System.out.println(userOpt);
            //nested switch to determine userOpt
            switch (userOpt)
            {
            case 1:
                // calls the engineCost method
                addCost = engineCost();
                //adds the value received to a total value
                ttlCost += addCost;
            break;

            case 2:
                //calls the colorOpt method
                addCost = colorOpt();
                //adds the value received to a total value
                ttlCost += addCost;
            break;

            case 3:
                //calls the tireOpt method
                addCost = tireOpt();
                //adds the value received to a total value
                ttlCost += addCost;
            break;
            }
            //System.out.print(addCost + baseCar);
            keyboard.close();
        } 
        while (userOpt !=4);
        //once loop has finished the total of additional purchases is added to the base car price 
        System.out.print("Total cost: " + (ttlCost + baseCar));
    }
    // this method has options for the engine
    static double engineCost()
    {
        double engineCost = 0;
        double addCost =0;
        int engineOpt;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Engine Options: ");
        System.out.println("1. 8 cylinder: $5000.00");
        System.out.println("2. 6 cylinder: $3500.00");
        System.out.println("3. Exit");
        System.out.println("Enter Option: ");
        engineOpt = keyboard.nextInt();

        switch (engineOpt)
        {
        case 1:
            engineCost = 5000.00;
            addCost += engineCost;
        break;
        case 2:
            engineCost = 3500.00;
            addCost += engineCost;
        break;  
        case 3:
            engineCost = 0;
            addCost += engineCost;
        }
        keyboard.close();
        return addCost; 
    }
}

Upvotes: 0

Views: 108

Answers (1)

GBlodgett
GBlodgett

Reputation: 12819

This is because of the line: keyboard.close(); within your loop. This closes the Scanner after your first iteration. After this you try to call nextInt() which (from the docs) will throw:

IllegalStateException - if this scanner is closed

You should only close a resource after you are completely done with it.

That being said do not close System.in. The general rule is that if you did not open it, your should not close it.

Upvotes: 2

Related Questions