0

I am a beginner in Java and attempting to create a very basic program for car rental. The issue I run into is that when I run my code for the first time, it works as expected. Whenever I provide the userinput as Y after completing a car reservation, to continue for the next customer, the code stops with "Enter your name", the scanner is not able to read the characters from keyboard. Please provide the best possible solution to fix this.

import java.util.Scanner;

public class Application {
    static Scanner input = new Scanner(System.in);
    static CustomerActivities customer = new CustomerActivities();
    static CarInventory carInventory = new CarInventory();
    public static void main(String[] args) {
        userName();
        customer.displayWelcomeMessage();
        carInventory.carList();
        carInventory.displayCars();
        carOptions();
        carInventory.searchCarAvailability();
        carConfirmation();
        carInventory.rentCar();
        carInventory.getInventory();
        anotherCustomer();
        input.close();
    }

    public static void userName(){
        System.out.println("Enter your name");
        System.out.println("boom");
        String userName = input.nextLine();
        System.out.println("bane");
        customer.setUserName(userName);
    }

    public static void carOptions(){
        System.out.println("Enter the car you would like to Rent");
        int userCarOption = input.nextInt();
        carInventory.setUserCarOption(userCarOption);
    }

    public static void carConfirmation(){
        char userConfirmation = input.next().charAt(0);
        carInventory.setUserConfirmation(userConfirmation);
    }

    public static void anotherCustomer(){
        System.out.println("Do you have any other customer");
        char customerContinue = input.next().charAt(0);
        if(customerContinue =='Y'){
            System.out.println("You can start entering the next customer details");
            userName();
        }
    }
}
gowthamjs23
  • 322
  • 5
  • 16
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels Jul 03 '17 at 02:04
  • The above mentioned link, fixes the issue to read the input from keyboard. but after reading the input, for some reasons I am not able to set the username. please guide me, if I am missing something else. – gowthamjs23 Jul 03 '17 at 02:13

0 Answers0