Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Symbol trouble

 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a beginner at Java I know enough to know I have done something wrong but not enough to know how ot fix it.

Car = new Car(reg, make, model, colour, passenger_no);

Is getting the errors

File: C:\Documents and Settings\Lari\My Documents\Huddersfield\Programming\Coursework2\Vehicles.java [line: 67]
Error: cannot find symbol
symbol : variable Car
location: class Vehicles
File: C:\Documents and Settings\Lari\My Documents\Huddersfield\Programming\Coursework2\Vehicles.java [line: 67]
Error: cannot find symbol
symbol : variable passenger_no
location: class Vehicles

I have got another class called Car.java, which starts with public class Car {

Is there some reason Java can't find this class?

Any help would e gratefully received as I seem to have been battling all day with this.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Car is the type, then you also have to name the reference:

Car myCar = new Car(...);
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, thanks for that, I have now changed the code to:

Car car = new Car(reg, make, model, colour, passenger_no);
carList.add(car);

The only other problem with this bit I think (and hope) is

Error: cannot find symbol
symbol : variable passenger_no
location: class Vehicles

I have declared passenger_no in my Car.java file as follows:

private int passenger_no;

does this code need something else?
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking again it is the int variables that it can't find. Can you group int values with strings?
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any help would be much appreciated, I can't work out what else could need changing, and one of the symbols not found is a String...

(Vehicles.Java)
Service service = new Service(service_no, reg, date, miles, part_replaced);
serviceList.add(service);
-------------------------------------
(Service.Java)
private int service_no;
private String reg;
private String date;
private String miles;
private String part_replaced;

public Service(int servNo, String reg, String dat, String mil, String part)
{
service_no = servNo;
reg = reg;
date = dat;
miles = mil;
part_replaced = part;
--------------------------------------------
(Error)
Error: cannot find symbol
symbol : variable part_replaced
location: class Vehicles

 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is the variable you pass TO a method, the variable you call it IN the method, and in your case, a member variable called the same thing (i think).

You have your Service.java class. This says that all Service objects will have (among other things) a String part_replaced.

you have a constructor for the Service class, that takes in (among other things) a String part.

in your Vehicles.java, you call the constructor for making a new service. you pass into that constructor (among other things) a part_replaced.

does the compiler tell you which line it can't find the variable? I'm guessing it's the line in Vehicles.java. do you have that variable defined in there?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Service service = new Service(service_no, reg, date, miles, part_replaced);


Have you declared these variables in your Service class?
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Please take a look if the variable "part_replaced" is defined in class Vehicles... perhaps you defined "part" instead?
2. The "reg=reg" in your constructor isn't the best idea. It should be "this.reg=reg", because else you will not change the "content" of your member variable.


Please check if you declared passenger_no in your Vehicles class, too. Perhaps you just forgot?
 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still new to java here too. But question if it is being declared as private, I thought it would intially error as symbol not found being called from another class. If it is - private int passenger_no; wouldn't it have to be declared as a different variable for other classes to access it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic