Troubleshooting "Cannot find symbol" JAVA Error

In summary, The conversation is about a person having trouble with an error message "Cannot find symbol" while trying to fill a rectangle with a specific color in Java. They are also discussing the use of constructors and methods to double a given number."
  • #1
doug5289
12
0

Homework Statement



Why am I getting an error message "Cannot find symbol"

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
Because the compiler can't find the symbol. Try the tambourine.
 
  • #3
You are apparently using a member of a class that you haven't imported. Does the compiler give any more details about the symbol it can't find?
 
  • #4
I found what I was missing, it was declaring the color. I did have the import java.awt.color loaded but it did not find the symbol, as soon as I put in Color myColor; it compiled I do need to know how to set a color not using the word red or green etc with using the numbers (255, 200,150)...I can not seem to get the proper java language to make it compile...i have now g.2 Color(Color.red); want to change to numbers (255, 0, 0) thank you for your help
 
  • #5
The Color class has a number of constructors - see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html . One of them takes three int values representing the red, green, and blue components.

This should work -
Code:
Color myColor = new Color(255, 200, 150);
 
Last edited by a moderator:
  • #6
Worked perfect! End to a long stressful day and I have just begun...
 
  • #7
I am trying to fill a rectangle and no matter what I put it doesn't fill it up. Any idea's?

g.fillRect(10, 10, 80, 30);
 
  • #8
doug5289 said:
I am trying to fill a rectangle and no matter what I put it doesn't fill it up. Any idea's?

g.fillRect(10, 10, 80, 30);

Command looks ok. Only thing I can think of is that you didn't set the graphics context's color. Did you do something like this first?

g.setColor(myColor);

Or your background was drawn in the same color, perhaps?
 
  • #9
It might be filling, but unless you fill with a color different from the background color, you can't see it. Did you call g.setColor(...) before calling g.fillRect( ...)?
 
  • #11
Mark44 said:
LOL! Great minds think alike.:biggrin:
:smile:
 
  • #12
Already set color, using g2.draw(rect1); want to change to g2.fill(rec1); It will draw but won't fill
 
  • #13
Your earlier wrote g.fillRect, and now you have g2.fill(). Do you have two different graphics contexts g and g2?

You also have rect1 and rec1. Are these different?
 
  • #14
Only have g2 and trying to switch from draw to fill...
 
  • #15
If you used g2.drawRect(rect);
and it drew a rectangle that was visible, calling g2.fillRect(rect);
should give you one that is filled in.

It would help to see your code.
 
  • #16
I am getting this error while compiling this: Any help would sure be welcomed. I am in my first year and never dreamed it could be this hard.

E:\RistDouglas_DoubleMyNumberTester.java:21: cannot find symbol
symbol : constructor RistDouglas_DoubleMyNumber(int)
location: class RistDouglas_DoubleMyNumber
aDoubleMyNumberObject = new RistDouglas_DoubleMyNumber(num);
^


public class LastNameFirstName_DoubleMyNumberTester
{
//Class Non-Accessor/Non-Mutator Method
public static void main (String[] args)
{
//Method's Local Variables
int num, doubleNum;
int DoubleMyNumber,aDoubleMyNumberObject;
//Method's Statements
//Display the purpose of the program.
System.out.println("This program will take a programmer-defined " +
"number and double it.");
System.out.println(); //Prints a blank line.
//Initialize the number.
num = 7;
//Construct the object.
aDoubleMyNumberObject = new RistDouglas_DoubleMyNumber();
//Double the number.
RistDouglas_DoubleMyNumber.doubleIt();
//Get the result.
doubleNum = aDoubleMyNumberObject.getNumber();
//Display results.
System.out.println("The result of doubling the number " + doubleNum +
" is " + num + ".");
System.out.println(); //Prints a blank line.
 
  • #17
doug5289 said:
I am getting this error while compiling this: Any help would sure be welcomed. I am in my first year and never dreamed it could be this hard.

E:\RistDouglas_DoubleMyNumberTester.java:21: cannot find symbol
symbol : constructor RistDouglas_DoubleMyNumber(int)
location: class RistDouglas_DoubleMyNumber
aDoubleMyNumberObject = new RistDouglas_DoubleMyNumber(num);
^
You should put [ code] and [ /code] tags (without leading spaces) around your code. I have done this below.
doug5289 said:
Code:
public class LastNameFirstName_DoubleMyNumberTester
{
  //Class Non-Accessor/Non-Mutator Method
  public static void main (String[] args)
  {
    //Method's Local Variables
    int num, doubleNum;
    int DoubleMyNumber,aDoubleMyNumberObject;
    //Method's Statements
    //Display the purpose of the program.
    System.out.println("This program will take a programmer-defined " +
      "number and double it.");
    System.out.println(); //Prints a blank line.
    //Initialize the number.
    num = 7;
    //Construct the object.
    aDoubleMyNumberObject = new RistDouglas_DoubleMyNumber();
    //Double the number.
    RistDouglas_DoubleMyNumber.doubleIt();
    //Get the result.
    doubleNum = aDoubleMyNumberObject.getNumber();
    //Display results.
    System.out.println("The result of doubling the number " + doubleNum +
      " is " + num + ".");
    System.out.println(); //Prints a blank line.

Where is the code for your RistDouglas_DoubleMyNumber class? You are calling the constructor in this line:
aDoubleMyNumberObject = new RistDouglas_DoubleMyNumber();
 
  • #18
This is the class side of the program Thanks for your help, this side compiled.


public class RistDouglas_DoubleMyNumber
{
//Instance Variables
private int num, myNumber;
//Instance Constructor
public RistDouglas_DoubleMyNumber()


{
this.num = myNumber;
}
//Instance Mutator Method
public void setDouble(int doubleIt)
{
this.myNumber = myNumber * 2;
}
//Instance Accessor Method
public int getNumber()
{
return this.num;
}
}
 
  • #19
Here is all of the code you provided, using [ code] and [ /code] tags to make it more readable.
Code:
public class LastNameFirstName_DoubleMyNumberTester
{
  //Class Non-Accessor/Non-Mutator Method
  public static void main (String[] args)
  {
    //Method's Local Variables
    int num, doubleNum;
    int DoubleMyNumber,aDoubleMyNumberObject;
    //Method's Statements
    //Display the purpose of the program.
    System.out.println("This program will take a programmer-defined " +
      "number and double it.");
    System.out.println(); //Prints a blank line.
    //Initialize the number.
    num = 7;
    //Construct the object.
    aDoubleMyNumberObject = new RistDouglas_DoubleMyNumber();
    //Double the number.
    RistDouglas_DoubleMyNumber.doubleIt();
    //Get the result.
    doubleNum = aDoubleMyNumberObject.getNumber();
    //Display results.
    System.out.println("The result of doubling the number " + doubleNum +
      " is " + num + ".");
    System.out.println(); //Prints a blank line. 
    
    // Mark44: no more code was shown.
  }
}


public class RistDouglas_DoubleMyNumber
{
  //Instance Variables
  private int num, myNumber;
  //Instance Constructor
  public RistDouglas_DoubleMyNumber()
  {
    this.num = myNumber;
  }

  //Instance Mutator Method
  public void setDouble(int doubleIt)
  {
    this.myNumber = myNumber * 2;
  }

  //Instance Accessor Method
  public int getNumber()
  {
    return this.num;
  }
}
The main problems I see are the following:
1. You are using the RistDouglas_DoubleMyNumber as if it returned an int - it doesn't.
Code:
aDoubleMyNumberObject = new RistDouglas_DoubleMyNumber();

This constructor returns a RistDouglas_DoubleMyNumber object. Once you have this object, you can use the setDouble method on it to double a number.

2. The constructor should have a parameter - the value that is used to initialize num. A call to the constructor should look something like this.
Code:
RistDouglas_DoubleMyNumber numberObject = new RistDouglas_DoubleMyNumber(num);

3. The RistDouglas_DoubleMyNumber constructor doesn't work as it should. It sets the member variable num to the value in myNumber, which is uninitialized.

3. You have too many member variables in your RistDouglas_DoubleMyNumber class, which greatly increases the opportunities for confusion. There is no need to have both a num member and a myNumber member. Pick one and get rid of the other.

4. There is no connection between num in Main and num in your RistDouglas_DoubleMyNumber class. Setting num to 7 in Main has absolutely no effect on num in your class.

5. Your setDouble method is broken. It takes a doubleIt parameter but does nothing with it.

6. The names you chose are misleading and therefore confusing. The name setDouble implies to me that something is going to happen to a Double value, which is one of the types in Java. aDoubleMyNumberObject is not a good choice for a name. It misleads one to think that it is some kind of object, but it is only an int. DoubleMyNumber is not a good choice.
 
  • #20
Remember what Mark said about using CODE tags around your code to format it so we can read it more easily? :)
Code:
public class RistDouglas_DoubleMyNumber
{
  //Instance Variables
  private int num, myNumber;
  //Instance Constructor
  public RistDouglas_DoubleMyNumber()
  

{
  this.num = myNumber;
  }
  //Instance Mutator Method
  public void setDouble(int doubleIt)
  {
    this.myNumber = myNumber * 2;
  }
  //Instance Accessor Method
  public int getNumber()
  {
    return this.num;
  }
}

There's a few problems with that, and with how it's used. Perhaps fixing this side will let you figure out what's wrong in the code that uses it.

Firstly, the constructor should probably have a parameter. You aren't setting 'num' anywhere here. Setting num equal to something elsewhere won't affect the value inside your doubling class. You do a this.num = myNumber, but myNumber isn't even initialized yet, let alone set to something useful. Looks like myNumber should be passed into the constructor with a parameter.

The setDouble "mutator" is called as .doubleIt in your main code. So the names don't agree. Also, I'd think it's num you want do double, yes? In fact, I don't see a reason for myNumber to be declared in that class at all. Nor does it need a parameter, from what I can see.

A proper mutator for this class should probably take a parameter and set num equal to it. That would be in addition to a .doubleIt method. That is, if you intend to set num to something else and double it without recreating (re-instantiating) a new class, at least in theory.

The accessor looks ok, though.

EDIT: Curses, beat me to it while I was typing. LOL
 
  • #21
Anyone want to help? My class side compile but not sure if it is right! Tester side needs help..


public class LastNameFirstName_BankAccount
{
//Class's Instance Variables
private double balance;
private String name;
//Instance Constructor
//Student: Write the instance constructor header accessible
// to all classes/objects that will initialize the object's
// name to the explicit reference parameter passed into
// this method and initialize the object's balance to 0.
public LastNameFirstName_BankAccount(double balance)

{
//Student: Initialize the instance reference variable that stores
// the name reference with the reference of the appropriate
// explicit parameter.
this.balance = balance;
this.name = name;
//Student: Initialize the instance value variable that stores
// the balance to 0.
this.balance = 0;
}
//Instance Mutator Method
public void deposit(double amount)
{
this.balance += amount;
}
//Instance Mutator Method
public void withdrawal(double amount)
{
this.balance -= amount;
}
//Instance Accessor Method
public double getBalance()
{
return this.balance;
}
//Instance Accessor Method
//Student: Write the instance accessor method header accessible
// to all classes/objects that will return the
// object's name reference. This header will not use any
// explicit parameters.
public String getName()
{
//Student: Return the reference of the instance reference variable
// that refers to the name.
return getName();
}
}

public class LastNameFirstName_BankAccountTester
{
//Class's Non-Accessor/Non-Mutator Method
public static void main(String[] args)
{
//Method's Local Variables
//Student: Declare a local value variable that stores the
// deposit initialized to 1000.
deposit = new deposit(1000);
//Student: Declare a local value variable that stores the
// first withdrawal initialized to 500.
withdrawel = new withdrawel(500);
//Student: Declare a local value variable that stores the
// first withdrawal initialized to 400.
withdrawel = new withdrawel(400);
//Method's local variables
//Student: Write the assignment statement that will:
// 1) construct a "BankAccount" object with the
// explicit reference parameter "Jane Doe" and
// 2) assign the address returned from the constructor to
// a new appropriately named local reference variable
// of an appropriate type.
LastNameFirstName_BankAccount = new BankAccount("Jane Doe");
//Method's Statements
//Display the purpose of the program.
//Student: Finish writing the class method call of the PrintStream class
// to print the results of the program; the component that
// comprises the explicit parameter is the following:
// 1) A constant String reference
// "This program will create a bank account for ".
// 2) Call an instance method of the "BankAccount" object to
// return the reference to the object's name.
// 3) A constant String reference " and do the following:".
System.out.println("This program will create a bank account for " + BankAccount + " and do the following:");
//Student: Finish writing the class method call of the PrintStream class
// to print the results of the program; the component that
// comprises the explicit parameter is the following:
// 1) A constant String reference
// "1) Deposit $".
// 2) A local value variable that stores the deposit amount.
// 3) A constant String reference ".".
System.out.println("1) Deposit $" + deposit + ".");
//Student: Finish writing the class method call of the PrintStream class
// to print the results of the program; the component that
// comprises the explicit parameter is the following:
// 1) A constant String reference
// "1) Deposit $".
// 2) A local value variable that stores the first withdrawal
// amount.
// 3) A constant String reference ".".
System.out.println("2) Withdraw $" + "withDrawel" + " ");
//Student: Finish writing the class method call of the PrintStream class
// to print the results of the program; the component that
// comprises the explicit parameter is the following:
// 1) A constant String reference
// "1) Deposit $".
// 2) A local value variable that stores the second withdrawal
// amount.
// 3) A constant String reference ".".
System.out.println("3) Withdraw $" + "withDrawel" + " ");
//Student: Finish writing the class method call of the PrintStream class
// to print the results of the program; the component that
// comprises the explicit parameter is the following:
// 1) A constant String reference
// "4) Display to the console screen ".
// 2) Call an instance method of the "BankAccount" object to
// return the reference to the object's name.
// 3) A constant String reference
// "'s final balance and the expected final balance.".
System.out.println("4) Display to the console screen " + BankAccount + "'s final balance and the expected final balance.");
System.out.println(); //Prints a blank line.
//Make a deposit to the account.
//Student: Call a method of the "BankAccount" object to add
// a deposit to the object's balance; the explicit
// value parameter consists of a local value variable
// that stores the deposit.
double balance = this.balance + deposit();
//Make a withdrawal from the account.
//Student: Call a method of the "BankAccount" object to subtract
// a withdrawal from the object's balance; the explicit
// value parameter consists of a local value variable
// that stores the first withdrawal.
double balance = balance - withdrawel();
//Make a withdrawal from the account.
//Student: Call a method of the "BankAccount" object to subtract
// a withdrawal from the object's balance; the explicit
// value parameter consists of a local value variable
// that stores the second withdrawal.
double balance = balance - withdrawel();
//Display the results using values obtained from the object.
//Student: Finish writing the class method call of the PrintStream class to
// print the results of the program; the component that
// comprises the explicit parameter is the following:
// 1) Call an instance method of the "BankAccount" object to
// return the reference to the object's name.
// 2) A constant String reference
// "'s final balance is $".
// 3) Call an instance method of the "BankAccount" object to
// return the value of the object's balance.
// 4) A constant String reference ".".
// System.out.println("BankAccount + ('s final balance is $) + getBalance");
//Display the expected results using programmer-defined local value
//variables used on the "SavingsAccount" object.
//Student: Finish writing the class method call of the PrintStream class to
// print the results of the program; the component that
// comprises the explicit parameter is the following:
// 1) A constant String reference
// "The expected final balance should be $".
// 2) A mathematical expression that computes the final balance
// after interest has been added using:
// A) the local value variable that stores the
// programmer-defined deposit,
// B) the local value variable that stores the
// programmer-defined first withdrawal, and
// C) the local value variable that stores the
// programmer-defined second withdrawal.
// 3) A constant String reference ".".
// System.out.println("The expected final balance should be $" + ( deposit - withdrawel - withdrawel) + " ");
// System.out.println("The actual final balance is $ + account.getBalance()" + (".");
//Exit the program.
System.out.println(); //Prints a blank line.
System.exit(0);
}
}
 
  • #22
Why don't you help us out by using [ code] and [ /code] tags (omit the leading spaces)? Both grep and I have asked you to use them to make your code easier to read.

Also, there are so many comments (usually not a problem) in your tester class that it's hard to find the actual code you wrote.
 
Last edited:
  • #23
This is only my fourth week of class and I am not sure what you mean by code and code tags. This is the entire program with what I tried to enter along with the comments of what I am suppose to do for class with this project. The component side compiled but not the test side. thanks
 
  • #24
When you type your code in or copy it here, put a code tag at the beginning and a /code tag at the end. Both need to be inside a pair of brackets - [].

I didn't type them because they don't render when the page is viewed.
 
  • #25
If it won't compile, you need to tell us what the error message is. Saying "it won't compile" is just not helpful.

I can see one or two issues in your constructor. Look closely at what you're doing with variables there. You seem to be expecting two values in the constructor (from looking at the code inside), but you have only one parameter. And the one you do pass in, you later set to 0 anyways. Perhaps more will become apparent when we see it formatted properly in code tags (as explained by Mark).
 
  • #26
I see quite a few problems.
1. Your BankAccount constructor should have two parameters - yours has only one. The two parameters are the name of the account holder and the balance. According to the instructions you provided, the name parameter should be used to initialize the name member variable, and the balance should be initialized to 0.

2. withdrawal method - What happens if the amount withdrawn is larger than the account balance? A bank won't let you withdraw more than you have in your account.

3. getName method - I wouldn't advise calling this method, as it will cause the computer to run out of memory and/or crash.

4. Several variables in the tester class are never declared, including deposit and withdrawel (mispelled).
 
  • #27
What is a code tag?
 
  • #28
It's an HTML tag. Put [ code] on the line above your code and [ /code] after the last line. Don't include the space after [.
 
  • #29
Thanks. I'm working on it!
 
  • #30
While you're at it, it would be nice to split your code up into two blocks, each with their own code tags.
 

FAQ: Troubleshooting "Cannot find symbol" JAVA Error

1. What does the "Cannot find symbol" error mean?

The "Cannot find symbol" error in Java means that the compiler is unable to locate a variable, method, or class that has been referenced in the code. This could be due to a misspelled identifier, a missing import statement, or a scope issue.

2. How can I fix the "Cannot find symbol" error?

To fix the "Cannot find symbol" error, you should first check for any spelling errors in your code. Next, make sure that the referenced identifier is declared and initialized in the correct scope. If the identifier is from an external class, make sure to import it properly. If all else fails, try restarting your IDE or clearing the cache.

3. Why am I getting a "Cannot find symbol" error when the code used to work?

If the code used to work but is now showing a "Cannot find symbol" error, it is likely that the referenced identifier has been changed or removed. Check for any recent changes in the code or external libraries that could have caused this error.

4. Can a "Cannot find symbol" error be caused by a missing semicolon?

No, a missing semicolon would result in a different error. The "Cannot find symbol" error specifically refers to the inability to locate an identifier in the code.

5. Is there a way to prevent "Cannot find symbol" errors?

To prevent "Cannot find symbol" errors, it is important to carefully check for any spelling errors and ensure that all identifiers are properly declared and initialized. It is also helpful to regularly test and debug your code to catch any potential errors early on.

Similar threads

Replies
2
Views
1K
Replies
5
Views
2K
Replies
6
Views
1K
Replies
4
Views
3K
Replies
6
Views
1K
Replies
1
Views
1K
Replies
17
Views
1K
Replies
1
Views
1K
Back
Top