-1

Problem: Taking input from the user with scanner (sc), and if I don't user a blank sc.nextLine(), the previous line will be skipped. If I do use this blank fire, the variable is never collected for later use.

System.out.println("Please enter the Airline name: ");
    String airName = sc.nextLine();
    sc.nextLine(); <--------- 
    System.out.println("Please enter the Airline code: ");
    String airCode = sc.nextLine();
    System.out.println("Please enter the Delta Aircraft: ");
    String airCraft = sc.nextLine();
    System.out.println("Please enter the first class seat capacity: ");
    int firstClass = sc.nextInt();
    System.out.println("Please enter the business class seat capacity: ");
    int busiClass = sc.nextInt();
    System.out.println("Please enter the economy class seat capacity: ");
    int econClass = sc.nextInt();
    System.out.println("Airline name: " + airName);
    System.out.println("Airline code: " + airCode);
    System.out.println("Delta Aircraft: " + airCraft);
    //Splitting the first word from the rest of the string
    String arr[] = airCraft.split(" ", 2);
    String firstWord = arr[0];
    System.out.println(firstWord + " first class seat capacity: " + firstClass);
    System.out.println(firstWord + " business class seat capacity: " + busiClass);
    System.out.println(firstWord + " economy class seat capacity: " + econClass);
    //Airline object
    A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
    System.out.println(airName + " successfully added. Press Enter to continue.");
    sc.nextLine();//Press Enter to continue
    sc.nextLine();   
    //A8MainMenu.mainMenu(sc); //return to main menu after Enter. 
    return airline;

My output now:

Airline name: 
Airline code: QA
Delta Aircraft: Boeing 787
Boeing first class seat capacity: 16
Boeing business class seat capacity: 25
Boeing economy class seat capacity: 199
 successfully added. Press Enter to continue.
J Ben
  • 127
  • 9
  • You might have to ref. to this thread. https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo?rq=1 – Ganga B K Dec 18 '17 at 16:33
  • I have, I've tried using a blank fire. – J Ben Dec 18 '17 at 16:44

1 Answers1

0

I ended up having to place a blank sc.nextLine before I began going through the scanner.

 sc.nextLine();
    System.out.println("Please enter the Airline name:");
    String airName = sc.nextLine();
J Ben
  • 127
  • 9