0

I am working in the ParkingCarSimulator programming challenges of the starting out with java book but I found a problem and I can't really see what is wrong, it seems right and the same than all the other ones. So I am getting the user to input values for officer name, officer badge, car make, car model, etc. The program is compiling good and seems fine but when you run it and start entering the info, you will notice that entering the info for the officer name is good, same for the officer badge but next should be car's make but it just skips that and goes into car's model and I can't really see why, any ideas? here is the code(check 3rd println):

public class ParkingCarSimulator {
    
    public static void main(String[] arsg)
    {
        
        String officerName, Make, carModel, carColor, carLicense;
        int badgeNumber, minOnCar, minPurchased;
        double fine = 0;
        
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Enter the officer's name");
        officerName = keyboard.nextLine();
        
        System.out.println("Enter officer's badge number");
        badgeNumber = keyboard.nextInt();
        
        System.out.println("Enter the car's make");
        Make = keyboard.nextLine();
        
        System.out.println("Enter the car's model");
        carModel = keyboard.nextLine();
        
        System.out.println("Enter the car's color");
        carColor = keyboard.nextLine();
        
        System.out.println("Enter the car's liscence number");
        carLicense = keyboard.nextLine();
        
        System.out.println("Enter minutes on car");
        minOnCar = keyboard.nextInt();
        if(minOnCar <= 0) {
            System.out.println("Invalid Entry. Please try again.");
            minOnCar = keyboard.nextInt();
        }
        
        System.out.println("Enter the number of minutes purchased");
        minPurchased = keyboard.nextInt();
        if(minPurchased <= 0) {
            System.out.println("Invalid Entry. Please try again.");
            minPurchased = keyboard.nextInt();
        }
        
        if(minOnCar > minPurchased) {
            fine = 25.0;
        }
        
        if(minPurchased < minOnCar) {
            System.out.println("Car parking time has expired");
            System.out.println("\nTicket Data:");
            System.out.println("\nMake: " + Make);
            System.out.println("\nModel: " + carModel);
            System.out.println("\nColor: " + carColor);
            System.out.println("\nLiscence Number: " + carLicense);
            System.out.println("\nOfficer Name: " + officerName);
            System.out.println("\nBadge Number: " + badgeNumber);
            System.out.println("\nFine: " + fine);
            
        } else {
            System.out.println("The car parking minutes are valid");
        }
        
    }

}

1 Answers1

1

.nextLine() reads the "enter" you hit once you enter the value of badgeNumber. In order to fix this, you can do this in your code:

        System.out.println("Enter officer's badge number");
        badgeNumber = keyboard.nextInt();
        
        System.out.println("Enter the car's make");
        Make = keyboard.nextLine(); //this takes up the enter value when it is pressed
        Make = keyboard.nextLine(); //changing the value of Make to the string the user enters.

Here's another link that could help you: Scanner is skipping nextLine() after using next() or nextFoo()?

Dharman
  • 21,838
  • 18
  • 57
  • 107
TheSj
  • 363
  • 11
  • @TomRonaldo that's good to hear! also, you should click the green check mark so that this question is marked as answered. – TheSj Mar 26 '21 at 16:14