Results 1 to 18 of 18
  1. #1
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default 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;
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    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.

  3. #3
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    Did you look at the API doc for the Console class?

  5. #5
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    Can you copy and paste here the example from the API doc for the Console class with your questions about it?

  7. #7
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    Good luck.

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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.

  10. #10
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by billboardfamily View Post
    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.

    ok, in the method getSeatToCancel the assignment seat = Console.readInt(); is commented out. so delete the // and the code will work fine.

  12. #12
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    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 #

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by billboardfamily View Post
    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.
    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

  14. #14
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  15. #15
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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.

  16. #16
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    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

  17. #17
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by billboardfamily View Post
    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 #
    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.

  18. #18
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Errors with simple program... PLEASE HELP ME!!!
    By maxpower1000sa in forum New To Java
    Replies: 6
    Last Post: 05-03-2009, 11:55 PM
  2. [SOLVED] hello, java program with 2 errors, help
    By einstein1234 in forum New To Java
    Replies: 24
    Last Post: 04-10-2009, 08:50 AM
  3. Errors in Program (Right Triangle)
    By SupaStudy in forum New To Java
    Replies: 3
    Last Post: 03-26-2009, 10:42 AM
  4. Help with Errors in Inventory Program
    By ljk8950 in forum AWT / Swing
    Replies: 3
    Last Post: 08-08-2008, 11:49 PM
  5. 3 errors and then terminate program
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 05-20-2008, 01:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •