AIrline Reservation - java

I am trying to do a basic Java Airline Reservation App. Here is my code, It seems that after I press '1' it is terminating and not running the rest of the code. I am not sure if it is something wrong with my loop or why it is terminating. Please if anyone has any ideas or answers I would love to hear them! thanks
import java.util.Scanner;
public class Reservation {
boolean[] seat = new boolean[11];
Scanner input = new Scanner(System.in);
public void start() {
while (true) {
makeReservation();
}
}
public void makeReservation() {
System.out.println("Please type 1 for first class and type 2 for economy");
int section = input.nextInt();
if (section == 1) {
firstClassSeat();
} else {
economySeat();
}
}
public void firstClassSeat() {
for (int count = 1; count <= 5; count++) {
if (seat[count] = false) {
seat[count] = true;
System.out.printf("First Class. Seat# %d\n", count);
break;
} else if (seat[10] == true) {
if (seat[5] == true) {
} else {
System.out.println("First class is fully booked, would you like an econmy seat");
int choice = input.nextInt();
if (choice == 1) {
firstClassSeat();
start();
} else {
System.out.println("Next flight is in 3 hours");
System.exit(0);
}
}
}
}
}
public void economySeat() {
for (int count = 6; count <= 10; count++) {
if (seat[count] = false) {
seat[count] = true;
System.out.printf("First Class. Seat# %d\n", count);
break;
} else if (seat[10] == true) {
if (seat[5] == true) {
} else {
System.out.println("Economy is fully booked. Would you like First Class? 1 for Yes 2 for No");
int choice = input.nextInt();
if (choice == 1) {
firstClassSeat();
start();
} else {
System.out.println("Next flight is in 3 hours");
System.exit(0);
}
}
}
}
}
}

First off, there is no need to compare booleans inside an if. seats[i] == false is the same as !seats[i].
Nothing is happening because you have an assignment:if(seat[count]=false instead of comparison:if(seat[count]==false. That's the first error, after fixing that you will start assigning seats.
Next, you're calling firstClassSeat inside the same method, when I think you wanted to call economySeat (when first class is full). You need to fix the logic behind the checks and economy/first class suggestions.

Related

how to print number of stars depending on user choose

I've been trying practiceIt problems and expand them a little - and I'm stuck.
The code will give results in as many stars as it's necessary, but I do not know how to make user decide the value for n.
I've tried adding to both methods (main/starString) those code lines:
"Scanner input = new.Scanner(System.in);
int n = input.next();" [also input.nextInt]
but the code will note allow any input from console. Not to mention I've got no idea where shoud I add second println command to actually print result from this code...
help me please
import java.util.*;
public class printStars {
public static void main(String[]args) {
System.out.println("choose number and I wil show you 2^number stars");
}
public static String starString(int n) {
if (n < 0) {
throw new IllegalArgumentException();
} else if (n == 0) {
return "*";
} else {
return starString(n - 1) + starString(n - 1);
}
}
}
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Choose number and I wil show you 2^number stars: ");
System.out.println(starString(in.nextInt()));
}
public static String starString(int n) {
if (n < 0) {
throw new IllegalArgumentException();
} else if (n == 0) {
return "*";
} else {
return starString(n - 1) + starString(n - 1);
}
}
}
A sample run:
Choose number and I wil show you 2^number stars: 5
********************************
You should also be checking the input before you enter the method. Here is one approach that allows for improper input and reprompts the user to enter the correct value. This way, no exceptions need be caught.
Scanner in = new Scanner(System.in);
String prompt = "Choose number (or a char to end) \nand I will show you 2^number stars: ";
System.out.print(prompt);
while (in.hasNextInt()) {
int n = in.nextInt();
if (n > 0) {
System.out.println(starString(n));
} else {
System.out.print("Input must be greater than 0, try again: ");
continue;
}
System.out.print(prompt);
}
System.out.println("Bye!");
public static String starString(int n) {
if (n == 0) {
return "*";
} else {
return starString(n - 1) + starString(n - 1);
}
}

How to have exit return me to main menu(Java) [duplicate]

This question already has an answer here:
How to return to main menu when exit is inputted
(1 answer)
Closed 6 years ago.
Can someone show me how to have this code return you to main menu when 4 is pressed. I know that I have to do while loops but im not sure how to. Right now I have exit as a return so it closes the whole program but instead I want it to return to main menu and restart eventSelection.
import java.util.*;
public class SchedulingProgram
{
public static void main (String [] args)
{
eventSelection();
}
public static void eventSelection()
{
Scanner sc = new Scanner(System.in);
System.out.println("Select Scheduling Action");
System.out.print("Add and Event = 1. \nDisplay Events = 2. \nPrint Alert = 3. \nExit = 4. \nINPUT : ");
int actionSelect = sc.nextInt();
if (actionSelect >= 1 && actionSelect <= 4)
{
if (actionSelect == 1)
{
addEvent();
}
else if (actionSelect == 2)
{
displayEvent();
}
else if (actionSelect == 3)
{
printAlert();
}
else if (actionSelect == 4)
{
return;
}
}
else
{
System.out.println("Error : Choice " + actionSelect + " Does Not Exist.");
}
}
You can do this with an infinite loop like this, but user will have no option to completely exit your program, if you want a command to exit program, you can use return in that command.
public static void eventSelection()
{
while (true){
Scanner sc = new Scanner(System.in);
System.out.println("Select Scheduling Action");
System.out.print("Add and Event = 1. \nDisplay Events = 2. \nPrint Alert = 3. \nExit = 4. \nINPUT : ");
int actionSelect = sc.nextInt();
if (actionSelect >= 1 && actionSelect <= 4)
{
if (actionSelect == 1)
{
addEvent();
}
else if (actionSelect == 2)
{
displayEvent();
}
else if (actionSelect == 3)
{
printAlert();
}
else if (actionSelect == 4)
{
System.out.println("Returning to selection menu");// Do nothing, next loop will be executed
}
}
else
{
System.out.println("Error : Choice " + actionSelect + " Does Not Exist.");
}
}
}
Return some value from eventSelection().Check in main function what value is returned.If it is matching with your value then call again eventSelection()

Java Scanner ignores answer

Hi it's my first post here and I have a problem:
I have a program in which, at some point, asks the user if he wants to share the stars, and if he supposedly does, the program goes back to collecting them and after some time comes back to the question if he wants to share them again.
The problem is that, when the user comes back to that point, the program ignores whatever answer u give to it and goes to the "else answer block".
It looks like this:
"Do you want to share your stars?
yes
Please answer with yes or no"
Code:
import java.util.Random;
import java.util.Scanner;
public class App {
static Scanner skan = new Scanner(System.in);
static int starCount = 0;
static Random random = new Random();
public static void main(String[] args) {
starReaching();
}
static void starReaching() {
boolean starCollected = false;
int i = 0;
int j = random.nextInt(101);
while (i < j) {
i++;
System.out.println("Stars are out of reach");
}
if (i > j || i == j) {
starCollected = true;
}
if (starCollected == true) {
starCollector();
}
}
static void starCollector() {
System.out.println("You caught a star !");
starCount++;
if (starCount == 10) {
System.out.println("You have 10 stars ! :)");
System.out.println("Do you want to share your stars?");
String line = skan.nextLine();
if (line.equals("yes")) {
skan.reset();
starGiver();
} else if (line.equals("no")) {
wishMaker();
} else {
System.out.println("Please answer with yes or no");
System.exit(0);
}
} else {
starReaching();
}
}
static void starGiver() {
System.out.println("How many do you want to share?");
int starsToShare = skan.nextInt();
if (starsToShare < 10 || starsToShare == 10 && starsToShare > 0) {
starCount = starCount - starsToShare;
System.out.println("Stars shared !");
System.out.println("You now have " + starCount + " stars");
System.out.println("Go collect them again!");
starReaching();
} else if (starsToShare > 10) {
System.out.println("You don't have enough stars to share that much!");
starGiver();
} else {
System.out.println("That's not a valid option");
starGiver();
}
}
static void wishMaker() {
}
}
Every time you call skan.nextLine() you read a new line and advance the scanner's pointer, and this is the reason your code is failing: you're calling this too often.
Instead of
if (skan.nextLine().equals("yes")) {
skan.reset();
starGiver();
} else if (skan.nextLine().equals("no")) {
wishMaker();
} else {
do:
String line = scan.nextLine(); // read from Scanner **once** only
if (line.equals("yes")) {
skan.reset();
starGiver();
} else if (line.equals("no")) {
wishMaker();
} else {
Okay i've found the little bugger, i was using skan.nextInt without using skan.nextLine after that, thank you for the quick help, much love

Unknown NullPointerException

I'm writing a simple class to simulate an airplane reservation system. Unfortunately, I keep having my plan-to-hog-all-first-class-seats glee interrupted by multiple null pointer exceptions, and I can't find them anywhere. Here is my completed work:
import java.util.*;
public class airplane
{
boolean[] seats;
Scanner scan = new Scanner(System.in);
public airplane(int num_of_seats)
{
boolean[] seats = new boolean[num_of_seats];
for (int counter = 0; counter < num_of_seats; counter++)
{
seats[counter] = true;
}
}
public boolean seatFirstClass(int seat)
{
if ( seat <= seats.length / 4 )
return true;
else
return false;
}
public void seatsReserveFirstClass()
{
for (int seat = 0; seat < seats.length; seat++)
{
if (seats[seat] == true && seat <= seats.length / 4)
{
seats[seat] = false;
System.out.print("I have reserved one seat in first class. Please tell the passenger to enjoy their flight with these airlines! ");
break;
}
}
System.out.print("I could not find a seat in first class. Can the passenger switch to economy? (0 for no, 1 for yes) ");
int choice = scan.nextInt();
if (choice == 0)
System.out.print("I could not add passenger due to lack of a seat.");
else if (choice == 1)
{
this.seatsReserveEconomy();
}
}
public void seatsReserveEconomy()
{
for (int seat = 0; seat < seats.length; seat++)
{
if (seats[seat] == true && seat > seats.length / 4)
{
seats[seat] = false;
System.out.print("I have reserved a seat in economy. Please tell the passenger to enjoy their flight with these airlines! ");
break;
}
}
System.out.print("I could not find a seat in economy. Can the passenger switch to first class? (0 for no, 1 for yes) ");
int choice = scan.nextInt();
if (choice == 0)
System.out.print("I could not add the passenger due to a lack of seats.");
else if (choice == 1)
this.seatsReserveFirstClass();
}
public void planeClear()
{
for (int seat = 0; seat < seats.length; seat++)
{
seats[seat] = true;
}
}
public void seatUnReserve(int seat)
{
if (seat < seats.length)
{
if (seats[seat] == false)
{
seats[seat] = true;
System.out.printf("Seat %d has been unreserved.\n", seat);
}
else
{
System.out.print("The seat you have entered is already unreserved.\n");
}
}
else
{
System.out.printf("There is no seat number %d on this plane.\n", seat);
}
}
}
and here is a main class to test it:
public class Test
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
boolean check = false;
System.out.print("How many seats does the plane have? ");
int num_of_seats = scan.nextInt();
airplane Airplane = new airplane(num_of_seats);
while (check == false)
{
System.out.print("Please enter 0 to reserve a first class seat, 1 to reserve an economy seat,\n2 to cancel a reservation, 3 to clear the plane,\nand 4 to quit the program. ");
int choice = scan.nextInt();
switch(choice)
{
case 0:
Airplane.seatsReserveFirstClass();
break;
case 1:
Airplane.seatsReserveEconomy();
break;
case 2:
System.out.print("Which seat should I unreserve? ");
int seat = scan.nextInt();
Airplane.seatUnReserve(seat);
break;
case 3:
Airplane.planeClear();
break;
case 4:
check = true;
break;
}
}
}
}
If this is run, the dreaded NullPointException error shows up, and I'm not sure what calls this beast.
Exception in thread "main" java.lang.NullPointerException
at airplane.seatsReserveFirstClass(airplane.java:22)
at Test.main(Test.java:15)
this is a field member:
boolean[] seats;
Scanner scan = new Scanner(System.in);
public airplane(int num_of_seats)
{
here you shadow the field member with a local variable, you are not touching the other one
boolean[] seats = new boolean[num_of_seats];
for (int counter = 0; counter < num_of_seats; counter++)
{
seats[counter] = true;
}
}
it should look like this:
public airplane(int num_of_seats)
{
seats = new boolean[num_of_seats];
for (int counter = 0; counter < num_of_seats; counter++)
{
seats[counter] = true;
}
}
Herman's quick eye found answer for you allready!! Anyway I extend the help for future...
You can use Java IDE , eclipse is good one, to develop programs. One of the very important feature, I believe, is debugging, which enables you to completely control your application and trace exception quickly.
Here is quick tutorial how to debug an application from eclipse ( Refer 4.3 for Exception breakpoints)
http://www.vogella.com/articles/EclipseDebugging/article.html

Skips over code

The problem occurs in the seventh section. It will not allow me to read in a state and instead prints the else statement.
import java.util.Scanner;
public class ifElseStatements
{
public static Scanner in = new Scanner (System.in);
public static void main (String[] args)
{
one();
two();
three();
four();
five();
six();
seven();
eight();
}
public static void one()
{
System.out.print("grade: ");
int number = in.nextInt();
if(number > 70 && number <=100)
{
System.out.println("You're passing");
}
else
{
System.out.println("you're not passing!");
}
}
public static void two()
{
System.out.print("Number please");
int b = in.nextInt();
if(b <=50)
{
System.out.println("Go");
}
else
{
System.out.println("STOP");
}
}
public static void three()
{
System.out.print("Integer please >");
int c = in.nextInt();
if(c%2 == 0)
{
System.out.println("Even");
}
else
{
System.out.println("odd");
}
}
public static void four()
{
System.out.print("Integer please");
int d = in.nextInt();
if(d%5 == 0)
{
System.out.println("Multiple of 5");
}
else
{
System.out.println("Not a multiple of 5");
}
}
public static void five()
{
System.out.print("number please");
int e = in.nextInt();
if(e< 10)
{
System.out.println("one digit");
}
else if(e>= 10 && e<100)
{
System.out.println("two digits");
}
else
{
System.out.println("three digits");
}
}
public static void six()
{
System.out.print("Jersey Number");
int f = in.nextInt();
if(f == 12 || f == 80 || f == 71)
{
System.out.println("That number is retired from the seattle seahawks");
}
else
{
System.out.println("");
}
}
public static void seven()
{
System.out.print("a state is");
String g = in.nextLine();
if(g .equals ("Washington") || g .equals ("Oregon") || g .equals ("Idaho"))
{
System.out.println("That state is in the PNW!");
}
else
{
System.out.println("You should move to the PNW; Its great here!");
}
}
public static void eight()
{
System.out.print("drink size (SHORT, TALL, GRANDE, VENTI)");
String h = in.nextLine();
if(h .equals ("SHORT"))
{
System.out.println("8");
}
else if(h .equals ("TALL"))
{
System.out.println("12");
}
else if(h .equals ("GRANDE"))
{
System.out.println("16");
}
else if(h .equals ("VENTI"))
{
System.out.println("20");
}
}
}
This is what it looks like when I run the code.
grade: 70
you're not passing!
Number please12
Go
Integer please >30
Even
Integer please15
Multiple of 5
number please33
two digits
Jersey Number12
That number is retired from the seattle seahawks
a state isYou should move to the PNW; Its great here!
drink size (SHORT, TALL, GRANDE, VENTI)TALL
12 oz
The previous step just does in.nextInt(), which reads the next int, but doesn't consume the EOL character. So, the step7 method reads the next line, and consumes the EOL that hasn't been consumed by the previous step.
You should add in.nextLine() in step6 to consume the EOL character, and not just the integer.
This is a common issue resulting from you using both Scanner.nextInt() and Scanner.nextLine(), which doesn't always work as you would want it to. You can find an explanation for this by going through JavaDoc to get an idea how Scanners work, but for a simple fix just replace instances of
in.nextInt();
with
Integer.parseInt(in.nextLine());

Categories

Resources