• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Need some help with Java program Airline.java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
carl martin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 77178
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 77178
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the misunderstanding, Ernest.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic