File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need some help with Java program Airline.java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need some help with Java program Airline.java" Watch "Need some help with Java program Airline.java" New topic
Author

Need some help with Java program Airline.java

carl martin
Greenhorn

Joined: Jul 29, 2010
Posts: 2
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

There are really only two problems here: the first one and the other three. In the first one, there's no variable "valid" that's in scope for that method (readChar()). All the other "valid" variables are declared inside methods (like in readInt()) so they're available only inside those methods. Is there just one line of code that you forgot to type? Do you understand what "scope" is?

For the other three compile errors: you need an instance of the Console class to call its non-static methods (i.e., new Console()), or the methods would have to be declared "static". Do you know what static methods are?


[Jess in Action][AskingGoodQuestions]
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

carl martin wrote:
ERRORS:

Airline.java:68: cannot find symbol
symbol : variable valid
location: class Airline.Console
valid = true;

Do you know what this error means? It means you're using a variable that hasn't been locally declared....
Let's look around line 68....



I'm assuming valid should be a boolean? declare it as such, and that should get rid of your first error.

As for the "non-static method readInt() cannot be referenced from a static context"
well, the main method is static. It's trying to call methods that are non static. If you make those methods static, the error will go away.

EDIT: EFH beat me cause I type slow


When you do things right, people won't be sure you've done anything at all.
carl martin
Greenhorn

Joined: Jul 29, 2010
Posts: 2
Thanks everyone. I was just wicked tired of working on that and needed a break. With your help I have those errors fixed, and the program is ALMOST working now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can't get new Console(); you have to use a method of the System class which returns the Console, but I can't remember its name just at the moment.

Beware of Console; if you open the application with "javaw" as you do for an executable .jar, you don't get a Console at all, and any attempt to use it results in a NullPointerException.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Campbell Ritchie wrote:You can't get new Console(); you have to use a method of the System class which returns the Console, but I can't remember its name just at the moment.

System.console().


[My Blog]
All roads lead to JavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Christophe Verré wrote:
Campbell Ritchie wrote:You can't get new Console(); you have to use a method of the System class which returns the Console, but I can't remember its name just at the moment.

System.console().


Well, yeah, if he hadn't defined his own class named "Console" to which I was referring.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sorry for the misunderstanding, Ernest.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need some help with Java program Airline.java