Reputation: 4050
In this program i needed the user to input yes / no to be set in another array class. But the string input doesnt work. I tried it with an integer and it worked but not with a string.
package airliner.boarding.system;
import java.util.Scanner;
public class BoardPlane {
private boolean firstClass[];
private boolean economyClass[];
private static Scanner input ;
public BoardPlane(){
firstClass = new boolean[5];
economyClass = new boolean[5];
input = new Scanner(System.in);
}
public void setSeat(int seatClass){
if(seatClass == 1){
fillFirstClass();
}else if(seatClass == 2){
fillEconomyClass();
}
}
private void fillEconomyClass(){
for(int counter = 0; counter < economyClass.length; counter++){
if(economyClass[counter] == false){
economyClass[counter] = true;
System.out.println("Your seat number is "+(++counter)+" in the economy class");
break;
}else if(counter == 4){
System.out.println("Economy class is filled. is it okay to place you in first class? YES/NO: ");
String choice = input.nextLine();
choice = choice.toUpperCase();
if(choice.equals("YES")){
switchSeats(2);
}else{
System.out.println("Next flight leaves in three hours.");
}
}
}
}
private void fillFirstClass(){
for(int counter = 0; counter < firstClass.length; counter++){
if(firstClass[counter] == false){
firstClass[counter] = true;
System.out.println("Your seat number is "+(++counter)+" in the first class");
break;
}else if(counter == 4 ){
System.out.println("First class is filled. is it okay to place you in economy class? YES/NO:");
String choice = input.nextLine();
choice = choice.toUpperCase();
if(choice.equals("YES")){
switchSeats(1);
}else{
System.out.println("Next flight leaves in three hours.");
}
}
}
}
private void switchSeats(int i){
if(i == 1){
for(int counter = 0; counter < economyClass.length; counter++){
if(economyClass[counter] == false){
economyClass[counter] = true;
System.out.println("Your seat number is "+(++counter)+" in the economy class");
break;
}else if(counter == 4){
System.out.println("Economy class is filled");
}
}
}else if(i == 2){
for(int counter = 0; counter < firstClass.length; counter++){
if(firstClass[counter] == false){
firstClass[counter] = true;
System.out.println("Your seat number is "+(++counter)+" in the first class");
break;
}else if(counter == 4){
System.out.println("First class is filled");
}
}
}
}
public static void main(String [] args){
BoardPlane plane = new BoardPlane();
for(int i = 0; i <= 10; i++){
System.out.println("Enter 1 for first class and 2 for economy class: ");
int userInput = input.nextInt();
plane.setSeat(userInput);
}
}
}
Upvotes: 0
Views: 103
Reputation: 123
i have same problem, when you use scanner, and put the message with System.out.println(), newLine not work correctly.I dont know why ?! when you make input = new Scanner(System.in) after out your message in console, it's work correct.
but u can make a method for give user input and in this method make a feresh scanner like this :
publis static String getUserInput(String message){
System.out.println(message);
input = new Scanner(System.in);
return input.nextLine();
}
Upvotes: 0
Reputation: 533530
When you call nextInt()
it only reads the number, not the rest of the line and not the new line you typed.
This means if you later call nextLine()
it will read what ever was after the number i.e. most likely nothing.
A simple work around is to ignore the rest of the line after the number.
int userInput = input.nextInt();
input.nextLine(); // ignore the rest of the line.
Upvotes: 1