Results 1 to 18 of 18
- 07-29-2010, 08:54 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Airline.java program ... getting errors, etc
I am new to Java, and I have been using a book to learn it myself. I have hit a few snags, but have gotten through most of them without too much of an issue. I just came upon a problem int he book that I am stumped on. I am getting errors, and I am not really sure I am even doing this right. Please help me out.
QUESTION IN THE BOOK:
Write an airline ticket reservation program called airline.java. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. Assume that tickets are reserved for only one flight. Create a linked list of flights with each node including a reference to a linked list of passengers.
ERRORS:
Airline.java:68: cannot find symbol
symbol : variable valid
location: class Airline.Console
valid = true;
^
Airline.java:146: non-static method readInt() cannot be referenced from a static context
choice = Console.readInt();
^
Airline.java:230: non-static method readString() cannot be referenced from a static context
return Console.readString();
^
Airline.java:243: non-static method readInt() cannot be referenced from a static context
seat = Console.readInt();
^
4 errors
CODE
Java Code:import java.io.*; import java.util.*; public class Airline { /** * The number of seats on the plane */ static final int MAX_SEATS = 8; public Airline() { } public void main(String[] args) { boolean done = false; String[] seats = new String[MAX_SEATS]; // the passenger list initializeSeats(seats); do { printMainMenu(); int choice = getMainMenuChoice(); // choice off of the main menu switch (choice) { case 1: makeReservation(seats); break; case 2: cancelReservation(seats); break; case 3: printSeatingChart(seats); break; case 4: done = true; break; } } while (!done); } //CONSOLE public class Console { private BufferedReader in; private String integerReprompt = "Invalid integer. Try again: "; private String doubleReprompt = "Invalid double. Try again: "; private String charReprompt = "Invalid character. Try again: "; { in = new BufferedReader(new InputStreamReader(System.in)); } public String readString() { String s = null; s = in.readLine(); return s; } public char readChar() { char c = 0; String s = in.readLine(); if (s.length() == 1) { c = s.charAt(0); valid = true; } else { System.out.print(charReprompt); } return c; } int readInt() { int i = 0; boolean valid = false; i = Integer.parseInt(in.readLine()); valid = true; System.out.print(integerReprompt); return i; } public double readDouble() { double d = 0.0; boolean valid = true; d = Double.parseDouble(in.readLine()); valid = true; valid = false; System.out.print(doubleReprompt); return d; } public void pause() { System.out.print("Press enter to continue..."); in.readLine(); } public void setIntegerReprompt(String prompt) { integerReprompt = prompt; } public void setDoubleReprompt(String prompt) { doubleReprompt = prompt; } public void setCharReprompt(String prompt) { charReprompt = prompt; } } //END CONSOLE /** * Print the main menu */ void printMainMenu() { System.out.println("\nMain Menu\n"); System.out.println("1. Make a reservation"); System.out.println("2. Cancel a reservation"); System.out.println("3. Print seating chart"); System.out.println("4. Quit"); System.out.println(); } /** * Get the user's choice off the main menu */ int getMainMenuChoice() { int choice; // choice entered boolean valid = false; // is choice valid? do { System.out.print("===> "); choice = Console.readInt(); if (1 <= choice && choice <= 4) { valid = true; } else { System.out.println("Invalid choice."); } } while (!valid); return choice; } /** * Initialize each element of seats to the empty string */ void initializeSeats(String[] seats) { for (int i = 0; i < seats.length; i++) { seats[i] = ""; } } /** * Make a reservation */ void makeReservation(String[] seats) { int seatIndex = findEmptySeat(seats); // index of first empty seat if (seatIndex == seats.length) { System.out.println("All seats are full. Sorry."); } else { String name = getPassengerName(); // passenger's name seats[seatIndex] = name; System.out.println(name + " has been assigned seat #" + (seatIndex+1)); } } /** * Cancel a reservation */ void cancelReservation(String[] seats) { int seatIndex = getSeatToCancel(); // index of seat to cancel reservation for if (isEmpty(seats, seatIndex)) { System.out.println("Seat #" + (seatIndex+1) + " has not been reserved for anyone"); } else { seats[seatIndex] = ""; System.out.println("Seat #" + (seatIndex+1) + " is now available"); } } /** * Print the seating chart */ void printSeatingChart(String[] seats) { System.out.println("\nSeating Chart\n"); for (int i = 0; i < seats.length; i++) { System.out.println((i+1) + ". " + seats[i]); } } /** * Find the index of the first empty seat on the plane. * If there are no empty seats, return seats.length */ int findEmptySeat(String[] seats) { for (int i = 0; i < seats.length; i++) { if (isEmpty(seats, i)) { return i; } } return seats.length; } /** * Yield whether seats[seatIndex] is an empty seat. */ boolean isEmpty(String[] seats, int seatIndex) { return seats[seatIndex].equals(""); } /** * Input a passenger's name */ String getPassengerName() { System.out.print("Enter the passenger's name: "); return Console.readString(); } /** * Input the number of a seat to cancel, return the * index of that seat. */ int getSeatToCancel() { boolean valid = false; // is the seat number valid? int seat; // seat number to cancel do { System.out.print("Enter the seat to cancel: "); seat = Console.readInt(); if (1 <= seat && seat <= MAX_SEATS) { valid = true; } else { System.out.println("Invalid seat number"); } } while (!valid); return seat-1; } }
- 07-29-2010, 09:28 PM #2
valid = true;
The variable: valid must be defined in scope so the compiler sees it.
How are you using the variable? Should it be a class member or a local method variable?
choice = Console.readInt();
How/where is the readInt() method defined? You are using it like it is defined as being a static method in the Console class? The compiler disagrees.
You need to define a Console object and use the reference to it vs the way you are using it. Read the API doc for the Console class, it has an example of how to use it.
- 07-29-2010, 09:50 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
The whole Console class is something that I was really unsure about using. Honestly, a friend of mine was *trying* to help me out on this, and this was his suggestion, but we never got this to work. I am at a loss on this program. I feel that it is close, but I am not skilled enough to fix the errors/make it totally work. I am looking for someone who knows what they are doing to really point me in the right direction on this one...it is pretty complicated considering the lesson is only on Linked Lists...seems far above that to me. IDK, My head hurts from looking at this code.
- 07-29-2010, 09:52 PM #4
Did you look at the API doc for the Console class?
- 07-29-2010, 09:54 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Yes, I did. That confused me more, unfortunately. I think I have been trying this program for way too long. Need a break from it, but I would like to get through it so I can not think about it.
- 07-29-2010, 09:56 PM #6
Can you copy and paste here the example from the API doc for the Console class with your questions about it?
- 07-29-2010, 09:58 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Maybe later, I don;t have time for cute little games. I need to get through this. I am taking a break from this now.
- 07-29-2010, 10:12 PM #8
Good luck.
- 07-29-2010, 11:08 PM #9
i made some main corrections to your code:
1. all methods in the class Console are static
2. your initialization block for the Console never run, because you never instantiate the Console class. in order to run a initialization block without an instance of it declare it as static, but note: static initialization run only once when the class is loaded.
3. all your readLine() statements are between try-catch-block now.
i made only the minimum corrections who was necessery to compile and run the code.
Here is the Airline class
Java Code:import java.io.*; import java.util.*; public class Airline { /** * The number of seats on the plane */ static final int MAX_SEATS = 8; public static void main(String[] args) { Airline airline = new Airline(); } public Airline() { boolean done = false; String[] seats = new String[MAX_SEATS]; // the passenger list initializeSeats(seats); do { printMainMenu(); int choice = getMainMenuChoice(); // choice off of the main menu switch (choice) { case 1: makeReservation(seats); break; case 2: cancelReservation(seats); break; case 3: printSeatingChart(seats); break; case 4: done = true; break; } } while (!done); } void printMainMenu() { System.out.println("\nMain Menu\n"); System.out.println("1. Make a reservation"); System.out.println("2. Cancel a reservation"); System.out.println("3. Print seating chart"); System.out.println("4. Quit"); System.out.println(); } /** * Get the user's choice off the main menu */ int getMainMenuChoice() { int choice; // choice entered boolean valid = false; // is choice valid? do { System.out.print("===> "); choice = Console.readInt(); if (1 <= choice && choice <= 4) { valid = true; } else { System.out.println("Invalid choice."); } } while (!valid); return choice; } /** * Initialize each element of seats to the empty string */ void initializeSeats(String[] seats) { for (int i = 0; i < seats.length; i++) { seats[i] = ""; } } /** * Make a reservation */ void makeReservation(String[] seats) { int seatIndex = findEmptySeat(seats); // index of first empty seat if (seatIndex == seats.length) { System.out.println("All seats are full. Sorry."); } else { String name = getPassengerName(); // passenger's name seats[seatIndex] = name; System.out.println(name + " has been assigned seat #" + (seatIndex + 1)); } } /** * Cancel a reservation */ void cancelReservation(String[] seats) { int seatIndex = getSeatToCancel(); // index of seat to cancel // reservation for if (isEmpty(seats, seatIndex)) { System.out.println("Seat #" + (seatIndex + 1) + " has not been reserved for anyone"); } else { seats[seatIndex] = ""; System.out .println("Seat #" + (seatIndex + 1) + " is now available"); } } /** * Print the seating chart */ void printSeatingChart(String[] seats) { System.out.println("\nSeating Chart\n"); for (int i = 0; i < seats.length; i++) { System.out.println((i + 1) + ". " + seats[i]); } } /** * Find the index of the first empty seat on the plane. If there are no * empty seats, return seats.length */ int findEmptySeat(String[] seats) { for (int i = 0; i < seats.length; i++) { if (isEmpty(seats, i)) { return i; } } return seats.length; } /** * Yield whether seats[seatIndex] is an empty seat. */ boolean isEmpty(String[] seats, int seatIndex) { return seats[seatIndex].equals(""); } /** * Input a passenger's name */ String getPassengerName() { System.out.print("Enter the passenger's name: "); return Console.readString(); } /** * Input the number of a seat to cancel, return the index of that seat. */ int getSeatToCancel() { boolean valid = false; // is the seat number valid? int seat = 0; // seat number to cancel do { System.out.print("Enter the seat to cancel: "); // seat = Console.readInt(); if (1 <= seat && seat <= MAX_SEATS) { valid = true; } else { System.out.println("Invalid seat number"); } } while (!valid); return seat - 1; } }
and here the Console class
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Console { private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private static String integerReprompt = "Invalid integer. Try again: "; private static String doubleReprompt = "Invalid double. Try again: "; private static String charReprompt = "Invalid character. Try again: "; public static String readString() { String s = null; try { s = in.readLine(); } catch (IOException ex) {} return s; } public static char readChar() { char c = 0; String s = null; try { s = in.readLine(); } catch (IOException ex) {} if (s.length() == 1) { c = s.charAt(0); // valid = true; } else { System.out.print(charReprompt); } return c; } public static int readInt() { int i = 0; boolean valid = false; try { i = Integer.parseInt(in.readLine()); } catch (IOException ex) {} valid = true; System.out.print(integerReprompt); return i; } public static double readDouble() { double d = 0.0; boolean valid = true; try { d = Double.parseDouble(in.readLine()); } catch (IOException ex) {} valid = true; valid = false; System.out.print(doubleReprompt); return d; } public void pause() { System.out.print("Press enter to continue..."); try { in.readLine(); } catch (IOException ex) {} } public static void setIntegerReprompt(String prompt) { integerReprompt = prompt; } public void setDoubleReprompt(String prompt) { doubleReprompt = prompt; } public void setCharReprompt(String prompt) { charReprompt = prompt; } }
compile both classes and start it with
java Airline
have fun.
- 07-29-2010, 11:44 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
This is almost working 100%. When you choose a menu option at the beginning of the program, you get an error "Invalid Integer" Please Enter Passenger name. You can enter the passenger name just fine, and it does remember it, and prints the seating chart correctly. When you try to cancel a reservation, you get an error repeating infinitely.
- 07-30-2010, 09:25 AM #11
- 07-30-2010, 06:16 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Thanks, that go trid of most of the problems. Unfortunately, whenever you choose an option in the menu you get this error: "Invalid integer. Try again:"
Then you are prompted to enter the passenger name, seat, or whatever is required for that menu item #
- 07-30-2010, 06:20 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,391
- Blog Entries
- 7
- Rep Power
- 17
Despite that fact that some repliers do hand out boilerplate code this is not a forum for spoonfeeding; you should to the work because you have a problem with your code; this is not a place where you can demand functionality to be implemented; rentacoder.org is a better place for that. We are able to help you when you are stuck with the code but it is you who has to write it.
kind regards,
Jos
- 07-30-2010, 06:28 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
I wrote all of the code! I am here because I can not figure out why it is not acting properly. Please look at my original post ... that is all me.
- 07-31-2010, 12:04 AM #15
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Even though you did write the original code, you still didn't show us your attempt to fix it before demanding more code from someone. Besides, it looks to us like you don't know what you are doing, so we want to teach you how to code, not give you answers.
- 07-31-2010, 01:23 AM #16
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
That is why I am here. I had 90% of the program correct, I was just missing a few logic pieces, and syntax errors, so I cam here to see if you guys had an idea of what I was doing wrong. I never asked one time for the code, and I was working on fixing the issues for DAYS, non-stop, even while you guys were helping me out. I do appreciate all the help. I came as far as I could in this....I could have asked early in the process, but I fought through as long as I could until I needed help
- 07-31-2010, 03:12 AM #17
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Well heck, look at the flipping code and walk through the logic:
Java Code:public static int readInt() { int i = 0; boolean valid = false; try { i = Integer.parseInt(in.readLine()); } catch (IOException ex) { } valid = true; System.out.print(integerReprompt); return i; }
You're ignoring the exception with your empty catch block:
Java Code:} catch (IOException ex) { // ***** never do this!!! }
and it's just printing out what you tell it to:
'Java Code:System.out.print(integerReprompt);
This is solved with simple effort and logic, not Java savvy.
- 07-31-2010, 03:14 AM #18
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Errors with simple program... PLEASE HELP ME!!!
By maxpower1000sa in forum New To JavaReplies: 6Last Post: 05-03-2009, 11:55 PM -
[SOLVED] hello, java program with 2 errors, help
By einstein1234 in forum New To JavaReplies: 24Last Post: 04-10-2009, 08:50 AM -
Errors in Program (Right Triangle)
By SupaStudy in forum New To JavaReplies: 3Last Post: 03-26-2009, 10:42 AM -
Help with Errors in Inventory Program
By ljk8950 in forum AWT / SwingReplies: 3Last Post: 08-08-2008, 11:49 PM -
3 errors and then terminate program
By hezfast2 in forum New To JavaReplies: 2Last Post: 05-20-2008, 01:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks