12 Replies - 335 Views - Last Post: 16 June 2015 - 04:23 AM Rate Topic: -----

#1 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Error Cannot Find Symbol

Posted 15 June 2015 - 04:16 PM

Basically, I have been trying to write out the following code just for practice, from a book I am learning Java from, but everytime I try to compile the source code, it is giving me errors. I would like to know if I am missing something, because it appears to be correct. The code below is the program I tried to compile.

 public class Transactions
{



   public static void main(String[] args)
   {
     Account acct1 = new Account("Ted Murphy", 72354, 102.56);
     Account acct2 = new Account("Jane Smith", 69713, 40.00);
     Account acct3 = new Account("Edward Demsey", 93757, 739.52);
     
     acct1.deposit(25.85);
     
     double smithBalance = acct2.deposit(500.00);
     
     System.out.println("Smith balance after deposit: " + smithBalance);
     
     System.out.println("Smith blance after withdrawal: " + acct2.withdraw(430.75, 1.50));
     
     acct1.addInterest();
     acct2.addInterest();
     acct3.addInterest();
     
     System.out.println();
     
     System.out.println(acct1);
     System.out.println(acct2);
     System.out.println(acct3);
     
    }
 }


Here is the errors I have been receiving:

Transactions.java:9: error: cannot find symbol
     Account acct1 = new Account("Ted Murphy", 72354, 102.56);
     ^
  symbol:   class Account
  location: class Transactions
Transactions.java:9: error: cannot find symbol
     Account acct1 = new Account("Ted Murphy", 72354, 102.56);
                         ^
  symbol:   class Account
  location: class Transactions
Transactions.java:10: error: cannot find symbol
     Account acct2 = new Account("Jane Smith", 69713, 40.00);
     ^
  symbol:   class Account
  location: class Transactions
Transactions.java:10: error: cannot find symbol
     Account acct2 = new Account("Jane Smith", 69713, 40.00);
                         ^
  symbol:   class Account
  location: class Transactions
Transactions.java:11: error: cannot find symbol
     Account acct3 = new Account("Edward Demsey", 93757, 739.52);
     ^
  symbol:   class Account
  location: class Transactions
Transactions.java:11: error: cannot find symbol
     Account acct3 = new Account("Edward Demsey", 93757, 739.52);
                         ^
  symbol:   class Account
  location: class Transactions
6 errors



Is This A Good Question/Topic? 0
  • +

Replies To: Error Cannot Find Symbol

#2 ChrisNt  Icon User is offline

  • cute bug


Reputation: 251
  • View blog
  • Posts: 811
  • Joined: 31-July 13

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 04:39 PM

Probably its something wrong at the way you are trying to compile it .
Which command are you using ? Are all your files ( .java ) in the same folder?

This post has been edited by ChrisNt: 15 June 2015 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 04:46 PM

I just simply used NetBeans to compile the code, just by making a new project, and copying the code from the book. Usually these types of errors are because of a spelling error for a class, indentation, or something like that and that is what has me a little puzzled by it. I don't know what word I spelt wrong in the code for it not to run.
Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt  Icon User is offline

  • cute bug


Reputation: 251
  • View blog
  • Posts: 811
  • Joined: 31-July 13

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 04:51 PM

Is your another class called Account ? Is your another class in the same src folder ?
Also try to use clean & build option at the top ( button ) things stuck sometimes :)

This post has been edited by ChrisNt: 15 June 2015 - 04:54 PM

Was This Post Helpful? 1
  • +
  • -

#5 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 04:52 PM

Some programs are in different folders, I have some saved to a certain file, and this one is saved to the desktop. Perhaps that may be another reason?

Thanks, I will try that and see if that could be the reason.
Was This Post Helpful? 0
  • +
  • -

#6 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 05:02 PM

View PostChrisNt, on 15 June 2015 - 04:51 PM, said:

Is your another class called Account ? Is your another class in the same src folder ?
Also try to use clean & build option at the top ( button ) things stuck sometimes :)/>


Hmm, I have the .java file, but I dont have the class file, that may be a part of the problem. Thanks for your advice :)
Was This Post Helpful? 0
  • +
  • -

#7 ChrisNt  Icon User is offline

  • cute bug


Reputation: 251
  • View blog
  • Posts: 811
  • Joined: 31-July 13

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 05:13 PM

So you have 2 .java files Account.java and Transactions.java in the same folder ..correct? It is expected to not have the .class files because those files are created when compilation is successful.hmmm..Take a screenshot of your netbeans project .
Was This Post Helpful? 0
  • +
  • -

#8 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 08:21 PM

View PostChrisNt, on 15 June 2015 - 05:13 PM, said:

So you have 2 .java files Account.java and Transactions.java in the same folder ..correct? It is expected to not have the .class files because those files are created when compilation is successful.hmmm..Take a screenshot of your netbeans project .


Actually I have just one java file called Transactions.java, I don't have an Account.java file. I tried making a separate folder, and placing the project file in there, clean it, and then run it to see if that helped, but I still get the same compile-time error.

Here are some screenshots:

Posted Image

When I begin writing the source code, the IDE immediately shows this:

Posted Image

as you can see from looking in the folder, and then in the file where Transactions.java is in, there is still class file in there:

Posted Image

There is no class file in the folder for Transactions.java
Was This Post Helpful? 0
  • +
  • -

#9 ndc85430  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 184
  • View blog
  • Posts: 716
  • Joined: 13-June 14

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 10:19 PM

Why don't you have an Account.java? Where do you expect the Account class to be defined, then?
Was This Post Helpful? 1
  • +
  • -

#10 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 10:45 PM

View Postndc85430, on 15 June 2015 - 10:19 PM, said:

Why don't you have an Account.java? Where do you expect the Account class to be defined, then?


Yes, it does seem like that is the issue. But looking at the example from the book made it appear like there was no relationship between the "Account.java" file and this "Transactions.java" file. That is why I decided just to write it in without includeing the "Account.java" file in the folder. But it does seem like they are related to each other.

From looking at all of the .java files I have in this chapter I have 1 "Account.java" file, along with several others, and then, I also have the "Transactions.java" file which I mentioned at the start. At first I tried makeing one independent "Transactions.java" file on my own independently, which gave me the compiling error I mentioned above. Now that you mentioned it, I tried putting both .java files in the same folder, compiling them, and it does work now.

However, I am wondering why they didn't mention in the book that one file was needed for the other .java file to compile.

This is the "account.java" file that seems to have a relationship the "Transactions.java" file btw:

import java.text.NumberFormat;

public class Account
{
   private final double RATE = 0.035;  // interest rate of 3.5%

   private long acctNumber;
   private double balance;
   private String name;

   //-----------------------------------------------------------------
   //  Sets up the account by defining its owner, account number,
   //  and initial balance.
   //-----------------------------------------------------------------
   public Account (String owner, long account, double initial)
   {
      name = owner;
      acctNumber = account;
      balance = initial;
   }

   //-----------------------------------------------------------------
   //  Deposits the specified amount into the account. Returns the
   //  new balance.
   //-----------------------------------------------------------------
   public double deposit (double amount)
   {
      balance = balance + amount;
      return balance;
   }

   //-----------------------------------------------------------------
   //  Withdraws the specified amount from the account and applies
   //  the fee. Returns the new balance.
   //-----------------------------------------------------------------
   public double withdraw (double amount, double fee)
   {
      balance = balance - amount - fee;
      return balance;
   }

   //-----------------------------------------------------------------
   //  Adds interest to the account and returns the new balance.
   //-----------------------------------------------------------------
   public double addInterest ()
   {
      balance += (balance * RATE);
      return balance;
   }

   //-----------------------------------------------------------------
   //  Returns the current balance of the account.
   //-----------------------------------------------------------------
   public double getBalance ()
   {
      return balance;
   }

   //-----------------------------------------------------------------
   //  Returns a one-line description of the account as a string.
   //-----------------------------------------------------------------
   public String toString ()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();
      return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
   }
}


Was This Post Helpful? 0
  • +
  • -

#11 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Re: Error Cannot Find Symbol

Posted 15 June 2015 - 11:16 PM

Thanks for the help anyways, I do appreciate it.
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2998
  • View blog
  • Posts: 12,850
  • Joined: 20-September 08

Re: Error Cannot Find Symbol

Posted 16 June 2015 - 02:46 AM

You need to stop thinking of files in folders and start thinking of classes in packages. That's the way Java works

Create a package under src called, say, com.yourdomainname.banking and put your source into it.
Was This Post Helpful? 2
  • +
  • -

#13 IntellectualCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-June 14

Re: Error Cannot Find Symbol

Posted 16 June 2015 - 04:23 AM

View Postg00se, on 16 June 2015 - 02:46 AM, said:

You need to stop thinking of files in folders and start thinking of classes in packages. That's the way Java works

Create a package under src called, say, com.yourdomainname.banking and put your source into it.


I do agree, this chapter deals with classes you would have to write on your own, and that probably had me confused about it in the first place. Thanks for the word of advice!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1