This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
can i ask whats the problem in my code? its done but the part of the yes or no which is this System.out.println("A refrigerator costs $2.50 extra each day would you like that? (Y/N)"); will be skipped and insted the System.out.println("Number of Days Staying :" + dys); will be printed.
import java.util.Scanner;
public class Test
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
String rt;
String a = new String ("G");
String b = new String ("P");
String c = new String ("L");
System.out.println(" Welcome to Easy Living Resort Hotel");
System.out.println("------------------------------------------");
System.out.println(" Room Type Daily Rate ");
System.out.println(" ~~~~~~~~~ ~~~~~~~~~~ ");
System.out.println(" G- Garden Pool View $125.00 ");
System.out.println(" P- Pool View $145.00 ");
System.out.println(" L- Lake View $180.00 ");
System.out.println("Please select your room type");
rt = scan.nextLine();
if(rt.equalsIgnoreCase("G")) {
System.out.println("Garden Pool View");
} else if(rt.equalsIgnoreCase("P")) {
System.out.println("Pool View");
} else {
System.out.println("Lake view");
}
int dys;
System.out.println("------------------------------------------");
System.out.println("Number of Days you are staying in: ");
dys = scan.nextInt();
String r;
String d = new String ("Y");
String e = new String ("N");
System.out.println("A refrigerator costs $2.50 extra each day would you like that? (Y/N)");
r = scan.nextLine();
if(r.equalsIgnoreCase("Y")) {
System.out.println("Y");
}else if (r.equals("N")) {
System.out.println("N");
}
System.out.println("Number of Days Staying :" + dys);
//System.out.println("
}
}
I would suggest you to go through this for detailed explanation.
https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/#:~:text=Why%20is%20Scanner%20skipping%20nextLine()%20after%20use%20of%20other%20next%20functions%3F,-Difficulty%20Level%20%3A%20Easy&text=The%20nextLine()%20method%20of%20java.&text=Scanner%20class%20advances%20this%20scanner,line%20separator%20at%20the%20end.
Here is a working example:
The problem was a missing scan.nextLine(). Otherwise it will take the \n of your previous input and skip the input. I also cleaned up your code. It is better to split up your code in methods.
import java.util.Scanner;
public class Reservation
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
String rt;
String a = "G";
String b = "P";
String c = "L";
showRoomPrices();
rt = scan.nextLine();
evaluateEnteredRoomType(rt);
int dys;
System.out.println("------------------------------------------");
System.out.println("Number of Days you are staying in: ");
dys = scan.nextInt();
String r;
String d = "Y";
String e = "N";
scan.nextLine();
System.out.println("A refrigerator costs $2.50 extra each day would you like that? (Y/N)");
r = scan.nextLine();
if(r.equalsIgnoreCase("Y")) {
System.out.println("Y");
}else if (r.equals("N")) {
System.out.println("N");
}
System.out.println("Number of Days Staying :" + dys);
//System.out.println("
}
private static void evaluateEnteredRoomType(final String rt) {
if(rt.equalsIgnoreCase("G")) {
System.out.println("Garden Pool View");
} else if(rt.equalsIgnoreCase("P")) {
System.out.println("Pool View");
} else {
System.out.println("Lake view");
}
}
private static void showRoomPrices() {
System.out.println(" Welcome to Easy Living Resort Hotel");
System.out.println("------------------------------------------");
System.out.println(" Room Type Daily Rate ");
System.out.println(" ~~~~~~~~~ ~~~~~~~~~~ ");
System.out.println(" G- Garden Pool View $125.00 ");
System.out.println(" P- Pool View $145.00 ");
System.out.println(" L- Lake View $180.00 ");
System.out.println("Please select your room type");
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Getting a main method error which is only affecting my retrieve salary method I'm not sure what to do to fix the issue I have added particular items but I can't get the error to go away. Is the only thing on my program that brings up an error and I am unable to continue. Image included of the only issues I'm encountering now
"Error: Main method not found in class, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"
Image included
public class finalproject {
public static class employeeCase {
EMPLOYEE[] employees;
int AMOUNT;
employeeCase(){
employees = new EMPLOYEE[100];
AMOUNT = 0;
}
private void loadEmployee() {
String ID = null;
int SALARY = 0;
Scanner sc = new Scanner(System.in);
System.out.println(" ");
System.out.println("How many employees do you want to load?: ");
int num = sc.nextInt();
for(int i = 0; i < num; i++){
// Display parallel arrays
System.out.println(" ");
System.out.println("Name: " + employees[i] + " " + "ID: " + ID + " " + "Salary: " + SALARY);
sc.close();
}
}
private int addEmployee() {
Scanner sc = new Scanner(System.in);
System.out.println(" ");
System.out.println("How many employees do you want to enter?: ");
AMOUNT = 0;
AMOUNT = sc.nextInt();
String Again1 = "no";
String Fname;
String ID;
int SALARY = 0;
do {
for(int i = 0; i < AMOUNT; i++) {
System.out.printf("Enter employee's first name: ");
Fname = sc.nextLine();
System.out.printf("Enter employee ID (5 digits): ");
ID = sc.next();
System.out.printf("Enter employee salary: ");
SALARY = sc.nextInt();
System.out.println(" ");
this.employees[this.AMOUNT] = new EMPLOYEE(Fname, ID, SALARY);
this.AMOUNT++;
sc.close();
} } while (Again1.equalsIgnoreCase("yes"));
return SALARY;
}
private void displayEmployee() {
for(int i = 0; i < AMOUNT; i++){
// Display parallel arrays
System.out.println(" ");
System.out.println(this.employees[i]);
}
}
private void retrieveSpecific() {
Scanner sc = new Scanner(System.in);
System.out.println(" ");
System.out.println("Enter employee ID: ");
String id = sc.next();
//Search for ID in all the stored employees
for(int i=0; i<this.AMOUNT; i++) {
if(id.contentEquals(this.employees[i].ID)) {
System.out.println(this.employees[i]);
sc.close();
}}}
private void retrieveSalary() {
Scanner scan = new Scanner(System.in);
System.out.println(" ");
System.out.println("Enter lowest employee salary: ");
int LSALARY = scan.nextInt();
System.out.println(" ");
System.out.println("Enter highest employee salary: ");
int HSALARY = scan.nextInt();
for(int i = 0; i < AMOUNT; i++) {
if(employees[i].SALARY >= LSALARY & employees[i].SALARY <= HSALARY) {
System.out.println(employees[i]);
scan.close();}}
}
public static void main(String[] args) {
employeeCase EmployeeData = new employeeCase();
Scanner sc = new Scanner(System.in);
int Select = 0;
do {
displayMenu();
System.out.print("Input your selection from the menu: ");
Select = sc.nextInt();
switch (Select) {
case 1 : EmployeeData.loadEmployee();
break;
case 2 : EmployeeData.addEmployee();
break;
case 3 : EmployeeData.displayEmployee();
break;
case 4 : EmployeeData.retrieveSpecific();
break;
case 5 : EmployeeData.retrieveSalary();
break;
case 6 : System.out.println("Thank you, goodbye!");
break;
default : System.err.println("Invalid Input");
break;
}
} while (Select != 6);
sc.close();
}
public static void displayMenu() {
System.out.println(" MENU");
System.out.println("============================================");
System.out.println("1: Load employees' data");
System.out.println("2: Add new employee");
System.out.println("3: Display all employees");
System.out.println("4: Retrieve specfic employee data");
System.out.println("5: Retrieve employee based on salary range");
System.out.println("6: Exit program");
}
}}
Your main method needs to be static. Change public void main to public static void main.
Access modifier for main method should be static here you define a non static main method
Replace your main method with
public static void main(String[] args){}
I have a program where I want to continue adding in an integer and string in a linked list. However when I print out the linked list it only prints out the last entered values and not the previous ones. So If I entered 3 Sally and then entered 6 Bob the linked list only prints out 6 bob. I want to be able to print out everything in the linkedlist no just the last entered.
public class Texteditor {
/**
* #param args the command line arguments
*/
static int myInt;
static String myString;
public Texteditor(int a, String s){
myInt = a;
myString = s;
}
public String toString(){
return myInt + " " + myString;
}
public static void main(String[] args) {
LinkedList<Texteditor> myLL = new LinkedList<Texteditor>();
int isExit = 0;
System.out.println("Hello Welcome to Your Personal Texteditor! ");
System.out.println("There are many options you can do with this text editor");
System.out.println("1. If you enter a line number with no text, the line number will be deleted.");
System.out.println("2. If you enter LIST alone the editor will print everything in the list with line number.");
System.out.println("3. If you enter RESEQUENCE the line numbers will be resequenced to start at 10.");
while(isExit ==0) {
// myLL = new LinkedList<Texteditor>();
System.out.println("");
System.out.println("Please enter the line number: ");
Scanner kb = new Scanner(System.in);
myInt = kb.nextInt();
System.out.println("Plese enter text as a string: ");
Scanner kb1 = new Scanner(System.in);
myString = kb1.nextLine();
Texteditor a1 = new Texteditor(myInt, myString);
myLL.add(a1);
System.out.println("Would you like to keep going? Enter yes or no: " );
Scanner kb2 = new Scanner(System.in);
if (kb2.next().equals("no")){
isExit = 1;
}
}
for (Texteditor element : myLL){
System.out.println(element + "\n");
}
}
}
Your myInt and myString are static, which means they're shared by instances. Make them non-static and the code should work correctly.
Also, don't recreate the Scanner every time in the loop. Once is enough.
The problem is that you are making the myInt and myString variables static. Remove the static modifier and then in your while loop, instead of referencing the class's myInt and myString variables, create local int and String variables instead.
public class Texteditor {
/**
* #param args the command line arguments
*/
int myInt;
String myString;
public Texteditor(int a, String s){
myInt = a;
myString = s;
}
public String toString(){
return myInt + " " + myString;
}
public static void main(String[] args) {
LinkedList<Texteditor> myLL = new LinkedList<Texteditor>();
int isExit = 0;
System.out.println("Hello Welcome to Your Personal Texteditor! ");
System.out.println("There are many options you can do with this text editor");
System.out.println("1. If you enter a line number with no text, the line number will be deleted.");
System.out.println("2. If you enter LIST alone the editor will print everything in the list with line number.");
System.out.println("3. If you enter RESEQUENCE the line numbers will be resequenced to start at 10.");
while(isExit ==0) {
// myLL = new LinkedList<Texteditor>();
System.out.println("");
System.out.println("Please enter the line number: ");
Scanner kb = new Scanner(System.in);
int myInt = kb.nextInt();
System.out.println("Plese enter text as a string: ");
Scanner kb1 = new Scanner(System.in);
String myString = kb1.nextLine();
Texteditor a1 = new Texteditor(myInt, myString);
myLL.add(a1);
System.out.println("Would you like to keep going? Enter yes or no: " );
Scanner kb2 = new Scanner(System.in);
if (kb2.next().equals("no")){
isExit = 1;
}
}
for (Texteditor element : myLL){
System.out.println(element + "\n");
}
}
}
The problem with your code is that the variables myInt and myString are static and hence they don't belong to each individual object (they belong to the class). Thus when you reference them here:
for (Texteditor2 element : myLL){
System.out.println(element + "\n");
}
You're calling the same values you set last n amount of times.
This should fix the problem:
Create a new TextEditorObject file:
public class TextEditorObject {
int myInt;
String myString;
public TextEditorObject(int a, String s){
myInt = a;
myString = s;
}
public String toString() {
return myInt + " " + myString;
}
}
Change Texteditor like so:
public class Texteditor {
public static void main(String[] args) {
int myInt;
String myString;
LinkedList<TextEditorObject> myLL = new LinkedList<TextEditorObject>();
int isExit = 0;
System.out.println("Hello Welcome to Your Personal Texteditor! ");
System.out.println("There are many options you can do with this text editor");
System.out.println("1. If you enter a line number with no text, the line number will be deleted.");
System.out.println("2. If you enter LIST alone the editor will print everything in the list with line number.");
System.out.println("3. If you enter RESEQUENCE the line numbers will be resequenced to start at 10.");
while(isExit ==0) {
// myLL = new LinkedList<Texteditor>();
System.out.println("");
System.out.println("Please enter the line number: ");
Scanner kb = new Scanner(System.in);
myInt = kb.nextInt();
System.out.println("Plese enter text as a string: ");
Scanner kb1 = new Scanner(System.in);
myString = kb1.nextLine();
TextEditorObject a1 = new TextEditorObject(myInt, myString);
myLL.add(a1);
System.out.println("Would you like to keep going? Enter yes or no: " );
Scanner kb2 = new Scanner(System.in);
if (kb2.next().equals("no")){
isExit = 1;
}
}
for (TextEditorObject element : myLL){
System.out.println(element + "\n");
}
}
}
I just started to code in Java and I have a question. After my "else" statement, I want to repeat my code again. How do I do that? Is there a keyword or something?
import java.util.Scanner;
public class UserInputStory {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
userinput:
System.out.println("Enter you name:");
String name = input.nextLine();
System.out.println("OK! Now enter your age:");
int age;
age = input.nextInt();
System.out.println("Good! And the city you live in, please:");
Scanner in = new Scanner(System.in);
String city = in.nextLine();
System.out.println("So, let's check");
System.out.println(
"Your name is " + name + ". You are " + age + " years old and you currently live in " + city + ".");
System.out.println("Is that right?");
Scanner inp = new Scanner(System.in);
String yesno = inp.nextLine();
if (yesno.equals("yes") || yesno.equals("Yes") || yesno.equals("YES")) {
System.out.println("Great job!");
}
else {
System.out.println("Let's try again then!");
}
}
}
Place the body of your code that you want repeating inside a while loop and break when your end-condition is true:
public static void main(String[] args) {
while(true) {
Scanner input = new Scanner(System.in);
userinput:
System.out.println("Enter you name:");
String name = input.nextLine();
System.out.println("OK! Now enter your age:");
int age;
age = input.nextInt();
System.out.println("Good! And the city you live in, please:");
Scanner in = new Scanner(System.in);
String city = in.nextLine();
System.out.println("So, let's check");
System.out.println("Your name is " + name + ". You are " + age + " years old and you currently live in " + city + ".");
System.out.println("Is that right?");
Scanner inp = new Scanner(System.in);
String yesno = inp.nextLine();
if (yesno.equals("yes") || yesno.equals("Yes") || yesno.equals("YES")) {
System.out.println("Great job!");
break;
}
else {
System.out.println("Let's try again then!");
}
}
}
You can envelop our whole code by:
while(1)
ut its not a good approach and there must be some condition applied (depending upon the xontext of your program) which can take you out of the loop
I am trying to make a simple text based operating system and I cant figure out why my code doesn't let me enter a command after the calculator class is done. It is supposed to continue executing the code until I type "off" but this is not the case. Eclipse says it is running but I cant do anything. can someone please help me?
here is my two classes:
public class Calculator extends Start{
public static void calStrt() {
System.out.print("\nEnter operator you wish to use: ");
StringInput = scan.nextLine();
if (StringInput.equals("+")) {
add();
} else if (StringInput.equals("-")) {
sub();
} else if (StringInput.equals("*")) {
mul();
} else if (StringInput.equals("/")) {
div();
} else {
System.out.println("\nSyntax error: Operator not recognized");
System.out.println("Please try again");
calStrt();
}
}
public static void add() {
System.out.print("\nEnter first number: ");
intInput = scan.nextInt();
int intVar1 = intInput;
System.out.print("\nEnter second number: ");
intInput = scan.nextInt();
int intVar2 = intInput;
System.out.println("\nAnswer: " + (intVar1 + intVar2));
}
public static void sub() {
System.out.print("\nEnter first number: ");
intInput = scan.nextInt();
int intVar1 = intInput;
System.out.print("\nEnter second number: ");
intInput = scan.nextInt();
int intVar2 = intInput;
System.out.println("\nAnswer: " + (intVar1 - intVar2));
}
public static void mul() {
System.out.print("\nEnter first number: ");
intInput = scan.nextInt();
int intVar1 = intInput;
System.out.print("\nEnter second number: ");
intInput = scan.nextInt();
int intVar2 = intInput;
System.out.println("\nAnswer: " + (intVar1 * intVar2));
}
public static void div() {
System.out.print("\nEnter first number: ");
intInput = scan.nextInt();
int intVar1 = intInput;
System.out.print("\nEnter second number: ");
intInput = scan.nextInt();
int intVar2 = intInput;
System.out.println("\nAnswer: " + (intVar1 / intVar2));
}
}
import java.util.Scanner;
class Start {
static Scanner scan = new Scanner(System.in);
static String StringInput;
static int intInput;
public static void main(String[] args) {
System.out.println("\nWelcome to RobOS");
passLoop: while (true) {
System.out.print("\nPlease enter password: ");
StringInput = scan.nextLine();
if (StringInput.equals("banana")) {
System.out.print("Logging in, please wait");
System.out.print(".");
System.out.print(".");
System.out.println(".");
System.out.println("\nWelcome User");
outerLoop: while (true) {
System.out.println("\nType \"help\" to see a list of programs");
StringInput = scan.nextLine();
innerLoop: while (true) {
if (StringInput.equalsIgnoreCase("cal")) {
Calculator.calStrt();
continue outerLoop;
} else if (StringInput.equalsIgnoreCase("guess")) {
GuessGame.guess();
continue outerLoop;
} else if (StringInput.equalsIgnoreCase("help")) {
System.out.println("\n\"cal\" uses the calculator");
System.out.println("\"guess\" plays guessing game");
System.out.println("\"help\" shows list of programs");
System.out.println("\"off\" turns RobOS off");
continue outerLoop;
} else if (StringInput.equalsIgnoreCase("off")){
break passLoop;
}
}
}
} else {
System.out.println("\nWrong password. Please try again");
continue passLoop;
}
}
}
}
Brent Nash is correct. To fix the error though, try using instead of scan.nextInt(): Integer.parseInt(scan.nextLine());
Hope this works
Your code is getting into an infinite loop. When you call StringInput = scan.nextLine(), the first time it works fine. I entered cal and I can run the calculator once. The problem is that the second time scan.nextLine() gets called, it's automatically returning an empty string "" as the value of StringInput. Your set of if/else statements in the while(true) have no way to handle this, so it just loops forever.
The deeper rationale is that you call scan.nextInt() to read in the numbers, but the problem is when you read in the second number for the calculator operation, there's still a "\n" sitting on System.in. As a result, when you loop around and call scan.nextLine() again, it doesn't prompt you for anything because it just reads that "\n" that's still sitting on System.in and then that sends you into an infinite loop.
I was tasked with creating a small application for college, and have created a simple booking system.
I'm having an issue in that when the program runs, for no reason I can fathom it's executing the print part of a method but then jumping to the next method without executing the in.next() part of the original method.
I've included here in order the abstract class, the class where the issue is occurring, the menu, and the 'Creation' class.
I've been informed before here my coding (at least naming conventions) are not the best so I apologize for this in advance.
When I altered the class so that wasn't an extension of raw_Booking it seemed to not have this issue but not sure if that's relevant.
import java.util.Scanner;
abstract class raw_Booking {
private static String fname;
private static String lname;
private static int HouseNo;
private static String Street;
private static String Postcode;
Scanner in = new Scanner(System.in);
raw_Booking(){
}
abstract public String Setfname();
abstract public String Setlname();
abstract public int SetHouseNo();
abstract public String SetStreet();
abstract public String SetPostcode();
abstract public String Verification();
}
The problem class (method is SetPostcode):
import java.util.Scanner;
import java.io.*;
class Hotel_Booking extends raw_Booking{
Scanner in = new Scanner(System.in);
private String TheVerification;
private int guests;
private String Roomtype;
private double numbRooms;
private double costStay;
private double nights;
private static String fname;
private static String lname;
private static int HouseNo;
private static String Street;
private static String Postcode;
Hotel_Booking() {
fname = Setfname();
lname = Setlname();
HouseNo = SetHouseNo();
Street = SetStreet();
Postcode = SetPostcode();
TheVerification = Verification();
Roomtype = setRoomtype();
guests = guests();
numbRooms = numbRooms();
nights = nights();
costStay = costStay();
Write_File();
}
public String Setfname(){
System.out.println();
System.out.print("Bookers first name please: ");
fname = in.next();
return fname;
}
public String Setlname(){
System.out.println();
System.out.print("Bookers surname please: ");
lname = in.next();
return lname;
}
public int SetHouseNo(){
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.print("Please enter your house number: ");
while (!sc.hasNextInt()) {
System.out.println();
System.out.print("You need to enter a number - please re-enter: ");
sc.next();
}
return sc.nextInt();
}
public String SetStreet(){
System.out.println();
System.out.print("Please enter your street: ");
Street =in.next();
return Street;
}
public String SetPostcode(){
System.out.println();
System.out.print("Please enter your postcode: ");
Postcode = in.next();
return Postcode;
}
public String Verification(){
System.out.println();
System.out.print("Please provide your car reg as verification: ");
TheVerification = in.next();
return TheVerification;
}
public String setRoomtype(){
System.out.println();
System.out.print("Would you like a Premiun or Regular room(s)? ");
Roomtype = in.next();
return Roomtype;
}
public int guests(){
System.out.println();
System.out.print("How many guests will be staying? ");
guests = in.nextInt();
return guests;
}
//For every 3 guests their must be 1 room at £50 per night.
//Premiun rooms = price doubled
public double numbRooms(){
for(int i=0;i < guests;i++){
numbRooms = numbRooms+1;
i = i + 2;
}
System.out.println();
System.out.println("You will require " + numbRooms + " number of rooms");
return numbRooms;
}
public double nights(){
System.out.println();
System.out.print("How many nights are you staying? ");
nights = in.nextDouble();
return nights;
}
public double costStay(){
if(Roomtype.equals("Regular")){
costStay = (nights * numbRooms)*50;
}
else{
costStay = (nights * numbRooms)*100;
}
System.out.println();
System.out.println("The total cost of your stay will be: " + costStay);
return costStay;
}
public void Write_File(){
System.out.println();
System.out.println("Your details will now be written to file.");
System.out.println();
try{
PrintWriter wr = new PrintWriter(new FileOutputStream("Hotel_Booking.txt",true));
wr.println("Name: " + fname+ " " + lname);
wr.println("Address line one: " + HouseNo + " " + Street);
wr.println("Postcode: " + Postcode);
wr.println("Roomtype: " + Roomtype);
wr.println("Number of rooms: " + numbRooms);
wr.println("Nights staying: " + nights);
wr.println("Total cost of stay: " + costStay);
wr.close();
}
catch (IOException e){
System.out.println("Error -- " + e.toString());
}
}
public static String Getfname(){
return fname;
}
}
Contains Interface/Menu.
public class Interface{
public static void Menu(){
System.out.println("Welcome to the booking system select an option");
System.out.println("1 - Create a new hotel booking");
System.out.println("2 - Create a new airline booking");
System.out.println("3 - Create a car hire booking");
System.out.println("4 - Create a amusement park ticket booking");
System.out.println("5 - Create a concert ticket booking");
System.out.println();
}
}
Main point of execution:
import java.util.ArrayList;
import java.util.Scanner;
public class Booking_Creation{
static Scanner in = new Scanner(System.in);
static ArrayList<Hotel_Booking> hotelbookings = new ArrayList<Hotel_Booking>();
static ArrayList<Flight_Booking> flightbookings = new ArrayList<Flight_Booking>();
static ArrayList<Car_Hire> carhirebookings = new ArrayList<Car_Hire>();
static ArrayList<Amusement_Park_Ticket> parkticket = new ArrayList<Amusement_Park_Ticket>();
static ArrayList<Concert_Ticket> concertticket = new ArrayList<Concert_Ticket>();
static int selection;
public static void main(String[] arguments){
Interface.Menu();
selection = in.nextInt();
// COULD ASK THE QUESTIONS HERE????
/// BUT THE INPUT BIT IS INSIDE THE OTHER CLASS
switch(selection){
case 1:
Hotel_Booking hb = new Hotel_Booking();
hotelbookings.add(hb);
break;
case 2:
Flight_Booking fb = new Flight_Booking();
flightbookings.add(fb);
break;
case 3:
Car_Hire ch = new Car_Hire();
carhirebookings.add(ch);
break;
case 4:
Amusement_Park_Ticket pt = new Amusement_Park_Ticket();
parkticket.add(pt);
break;
case 5:
Concert_Ticket ct = new Concert_Ticket();
concertticket.add(ct);
break;
}
}
}
Example output:
Welcome to the booking system select an option
1 - Create a new hotel booking
2 - Create a new airline booking
3 - Create a car hire booking
4 - Create a amusement park ticket booking
5 - Create a concert ticket booking
1
Bookers first name please: Herton
Bookers surname please: Ferford
Please enter your house number: 4
Please enter your street: Park Lane
Please enter your postcode:
Please provide your car reg as verification: KS23
After a quick glance to your sample execution, I think that the street name is parsed as two tokens, so the street name is set as "Tulloch" and when prompted for the postcode (as a string), "Park" is read.
It might be useful to set a pattern on the Scanner.next() method to force a full line as a single token ?