Results 1 to 20 of 27
- 01-09-2012, 02:34 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Error ' Cannot find symbol class UserAccount"
Why am I getting the error Error ' Cannot find symbol class UserAccount" is the code below? .
Java Code:// Create 10 account objects UserAccount[] UserAccountT = new UserAccount [10]; UserAccountT [0]= new UserAccount ();
The class is below.
Java Code:import java.util.*; class accountAR { Scanner input = new Scanner(System.in); // Create an instance of the class account Account UserAccount = new Account (); // Welcome method to prompt the user to input ID public void Welcome () { int p; System.out.println("Please enter your ID number\n"); p =input.nextInt(); // If id is incorrect print message or start mainMenu if (p > 10) { System.out.println ("Please re-enter your ID number\n"); } else { mainMenu();} } public void mainMenu(){ int selection; System.out.print("Welcome to the Automated Teller Machine!\n"); System.out.println("Select from the following menu options below:\n"); System.out.println("========================"); System.out.println("| [1] Check Balance |"); System.out.println("| [2] Withdrawal |"); System.out.println("| [3] Deposit |"); System.out.println("| [4] Exit |"); System.out.println("========================"); System.out.print("Please select your option now: "); selection =input.nextInt(); switch (selection){ case 1: viewBalance(); break; case 2: withdrawFunds(); break; case 3: depositFunds(); break; case 4: System.out.println("Thank you for using ATM! \n Goodbye! \n"); } } public void viewBalance() { int selection1; System.out.println("You have selected Balance.\n"); System.out.println("\t-- Your Current Balance is:$ " + UserAccount.getbalance()); System.out.println("Return to main menu? \n [1] for YES \n"); selection1 =input.nextInt(); switch (selection1){ case 1: mainMenu(); break; } } //Method to withdraw money public void withdrawFunds() { int withdrawSelection; System.out.println("Amount to withdraw: "); System.out.println("[1] - $20"); System.out.println("[2] - $40"); System.out.println("[3] - $50"); System.out.println("[4] - $100"); System.out.println("[5] - MAIN MENU"); System.out.print("Please select your option now: "); withdrawSelection =input.nextInt(); switch (withdrawSelection){ case 1: UserAccount.withdraw(20); mainMenu(); break; case 2: UserAccount.withdraw(40); mainMenu(); break; case 3: UserAccount.withdraw(50); mainMenu(); break; case 4: UserAccount.withdraw(100); mainMenu(); break; case 5: mainMenu(); break; } } // method for choosing how much to deposit public void depositFunds(){ int addSelection; System.out.println("Amount to deposit: "); System.out.println("[1] - $20"); System.out.println("[2] - $40"); System.out.println("[3] - $50"); System.out.println("[4] - $100"); System.out.println("[5] - MAIN MENU"); System.out.print("Please select your option now: "); addSelection =input.nextInt(); switch (addSelection){ case 1: UserAccount.deposit(20); mainMenu(); break; case 2: UserAccount.deposit(40); mainMenu(); break; case 3: UserAccount.deposit(50); mainMenu(); break; case 4: UserAccount.deposit(100); mainMenu(); break; case 5: mainMenu(); break; } } public accountAR () {} // Start main public static void main (String [] args){ // Create 10 account objects UserAccount[] UserAccountT = new UserAccount [10]; UserAccountT [0]= new UserAccount (); // loop to create 10 accounts with balance of 50 for (int x = 0; x<10;x++) { UserAccountT [x] = new UserAccount (); UserAccountT [x].setId (x); UserAccountT [x].setBalance(50); } // Start the welcome message Welcome (); } }
- 01-09-2012, 03:09 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Error ' Cannot find symbol class UserAccount"
What is a UserAccount? I guess it's supposed to be a class but the compiler can't find a class definition for a UserAccount.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-09-2012, 03:25 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Error ' Cannot find symbol class UserAccount"
HI Jos
In my viewBalance() method in class accounAR I am having issues referencing the getBalance method in accounts so I added Account UserAccount = new Account (); to the instance variable of accountAR.
Im new to this and obviously doing things wrong. My account class is below.
Java Code:import java.util.*; public class Account { private int id; private String name; private double balance; private int pinNumber; private boolean isActive; private Date dateCreated = new Date(); private double annualInterestRate; public Account (int a ) { int id = a; String name ; double balance; int pinNumber = 1386; boolean isActive = false; Date dateCreated = new Date(); double annualInterestRate = 4.4; } // no agr constructor public Account (){}; // set the pinNumber protected void setpinNumber (int input) { this.pinNumber = input; } protected void setId (int inputID) { this.id = inputID;} protected int getpinNumber () { return pinNumber; } protected boolean testID ( int entry) { if ( id != entry ) { isActive = true ; } else { System.out.println (" Please re-enter your PIN number"); isActive = false; } return isActive; } protected int getid () { return id;} protected String getname () { return name;} protected void setBalance ( double c) { this.balance = c;} protected double getbalance () { return balance;} protected boolean isActive () { return isActive;} protected void deposit( double amount ) { balance += amount; } public double withdraw( double amount ) { // See if amount can be withdrawn if (balance >= amount) { balance -= amount; return amount; } else // Withdrawal not allowed return 0.0; } protected void setannualInterestRate( double rate) { annualInterestRate = rate ; } // int for the 12 months of the year public double m = 12; // method to get the monthly interest rate protected double getmonthlyInterestRate() { return annualInterestRate / m ; } protected Date getDate () { return dateCreated ;} }
- 01-09-2012, 03:46 PM #4
Re: Error ' Cannot find symbol class UserAccount"
If you are getting errors, please copy the full text of the message and post it here.I am having issues
- 01-09-2012, 03:50 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Error ' Cannot find symbol class UserAccount"
UserAccount[] UserAccountT = new UserAccount [10];
UserAccountT [0]= new UserAccount ();
What class is UserAccount?
It's not the class you just posted...Last edited by Tolls; 01-09-2012 at 03:53 PM.
- 01-09-2012, 05:00 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Error ' Cannot find symbol class UserAccount"
I have edited the code to remove UserAccount. I have added an array of Account but I am now getting the error.
non-static method getbalance() cannot be referenced from a static context
non-static method withdraw(double) cannot be referenced from a static context
non-static method withdraw(double) cannot be referenced from a static context
non-static method withdraw(double) cannot be referenced from a static context
non-static method withdraw(double) cannot be referenced from a static context
non-static method deposit(double) cannot be referenced from a static context
non-static method deposit(double) cannot be referenced from a static context
non-static method deposit(double) cannot be referenced from a static context
non-static method deposit(double) cannot be referenced from a static context
Java Code:import java.util.*; class accountAR { Account Accounts = new Account(); Scanner input = new Scanner(System.in); // Welcome method to prompt the user to input ID public void Welcome () { int p; System.out.println("Please enter your ID number\n"); p =input.nextInt(); // If id is incorrect print message or start mainMenu if (p > 10) { System.out.println ("Please re-enter your ID number\n"); } else { mainMenu();} } public void mainMenu(){ int selection; System.out.print("Welcome to the Automated Teller Machine!\n"); System.out.println("Select from the following menu options below:\n"); System.out.println("========================"); System.out.println("| [1] Check Balance |"); System.out.println("| [2] Withdrawal |"); System.out.println("| [3] Deposit |"); System.out.println("| [4] Exit |"); System.out.println("========================"); System.out.print("Please select your option now: "); selection =input.nextInt(); switch (selection){ case 1: viewBalance(); break; case 2: withdrawFunds(); break; case 3: depositFunds(); break; case 4: System.out.println("Thank you for using ATM! \n Goodbye! \n"); } } public void viewBalance() { int selection1; System.out.println("You have selected Balance.\n"); System.out.println("\t-- Your Current Balance is:$ " + Account.getbalance()); System.out.println("Return to main menu? \n [1] for YES \n"); selection1 =input.nextInt(); switch (selection1){ case 1: mainMenu(); break; } } //Method to withdraw money public void withdrawFunds() { int withdrawSelection; System.out.println("Amount to withdraw: "); System.out.println("[1] - $20"); System.out.println("[2] - $40"); System.out.println("[3] - $50"); System.out.println("[4] - $100"); System.out.println("[5] - MAIN MENU"); System.out.print("Please select your option now: "); withdrawSelection =input.nextInt(); switch (withdrawSelection){ case 1: Account.withdraw(20); mainMenu(); break; case 2: Account.withdraw(40); mainMenu(); break; case 3: Account.withdraw(50); mainMenu(); break; case 4: Account.withdraw(100); mainMenu(); break; case 5: mainMenu(); break; } } // method for choosing how much to deposit public void depositFunds(){ int addSelection; System.out.println("Amount to deposit: "); System.out.println("[1] - $20"); System.out.println("[2] - $40"); System.out.println("[3] - $50"); System.out.println("[4] - $100"); System.out.println("[5] - MAIN MENU"); System.out.print("Please select your option now: "); addSelection =input.nextInt(); switch (addSelection){ case 1: Account.deposit(20); mainMenu(); break; case 2: Account.deposit(40); mainMenu(); break; case 3: Account.deposit(50); mainMenu(); break; case 4: Account.deposit(100); mainMenu(); break; case 5: mainMenu(); break; } } public accountAR () {} // Start main public static void main (String [] args){ // Create 10 account objects Account[] Account = new Account [10]; Account [0]= new Account (); // loop to create 10 accounts with balance of 50 for (int x = 0; x<10;x++) { Account [x] = new Account (); Account [x].setId (x); Account [x].setBalance(50); } // Start the welcome message } }
- 01-09-2012, 05:08 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Error ' Cannot find symbol class UserAccount"
I have got it to work by changing the Account to Accounts. The question I have now is how do I get viewBalance method () to
show the balance of array Account[0]?
- 01-09-2012, 05:10 PM #8
Re: Error ' Cannot find symbol class UserAccount"
Do you mean the contents of the one element at index 0show the balance of array Account[0]
or the contents of the whole array?
For the whole array the Arrays class's toString() method will format the array's contents into a String.
- 01-09-2012, 05:43 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Error ' Cannot find symbol class UserAccount"
It would be the balance of one element. The system prompts the user to enter an id.
If the id is entered incorrectly the user will be asked to enter a correct id. Once the id is accepted the main menu is
displayed. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money and 4 for exiting the main menu.
If the user tries to withdraw more money than is available the program reports a “ Not enough funds” message. Once you exit the system will prompt for an id again. So once the system starts, it will not stop.
- 01-09-2012, 05:47 PM #10
Re: Error ' Cannot find symbol class UserAccount"
Not sure what your problem is. If you have the index to the array, you can use that index to get the element.It would be the balance of one element
If you have the element then you can get the balance of that element.
Please explain where your problem is.
- 01-09-2012, 06:07 PM #11
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Error ' Cannot find symbol class UserAccount"
Well the problem I am having is the users input is not showing the balance of the array element just created when mainMenu() called.
Java Code:import java.util.*; class accountAR { Account Accounts = new Account(); Scanner input = new Scanner(System.in); // Welcome method to prompt the user to input ID public void Welcome () { int p; System.out.println("Please enter your ID number\n"); p =input.nextInt(); // If id is incorrect print message or start mainMenu if (p > 10) { System.out.println ("Please re-enter your ID number\n"); } else { mainMenu();} } public void mainMenu(){ int selection; System.out.print("Welcome to the Automated Teller Machine!\n"); System.out.println("Select from the following menu options below:\n"); System.out.println("========================"); System.out.println("| [1] Check Balance |"); System.out.println("| [2] Withdrawal |"); System.out.println("| [3] Deposit |"); System.out.println("| [4] Exit |"); System.out.println("========================"); System.out.print("Please select your option now: "); selection =input.nextInt(); switch (selection){ case 1: viewBalance(); break; case 2: withdrawFunds(); break; case 3: depositFunds(); break; case 4: System.out.println("Thank you for using ATM! \n Goodbye! \n"); } } public void viewBalance() { int selection1; System.out.println("You have selected Balance.\n"); System.out.println("\t-- Your Current Balance is:$ " + Accounts.getbalance()); System.out.println("Return to main menu? \n [1] for YES \n"); selection1 =input.nextInt(); switch (selection1){ case 1: mainMenu(); break; } } //Method to withdraw money public void withdrawFunds() { int withdrawSelection; System.out.println("Amount to withdraw: "); System.out.println("[1] - $20"); System.out.println("[2] - $40"); System.out.println("[3] - $50"); System.out.println("[4] - $100"); System.out.println("[5] - MAIN MENU"); System.out.print("Please select your option now: "); withdrawSelection =input.nextInt(); switch (withdrawSelection){ case 1: Accounts.withdraw(20); mainMenu(); break; case 2: Accounts.withdraw(40); mainMenu(); break; case 3: Accounts.withdraw(50); mainMenu(); break; case 4: Accounts.withdraw(100); mainMenu(); break; case 5: mainMenu(); break; } } // method for choosing how much to deposit public void depositFunds(){ int addSelection; System.out.println("Amount to deposit: "); System.out.println("[1] - $20"); System.out.println("[2] - $40"); System.out.println("[3] - $50"); System.out.println("[4] - $100"); System.out.println("[5] - MAIN MENU"); System.out.print("Please select your option now: "); addSelection =input.nextInt(); switch (addSelection){ case 1: Accounts.deposit(20); mainMenu(); break; case 2: Accounts.deposit(40); mainMenu(); break; case 3: Accounts.deposit(50); mainMenu(); break; case 4: Accounts.deposit(100); mainMenu(); break; case 5: mainMenu(); break; } } public accountAR () {} // Start main public static void main (String [] args){ accountAR a = new accountAR (); Scanner input1 = new Scanner(System.in); // Create 10 account objects Account[] Account = new Account [10]; Account [0]= new Account (); // loop to create 10 accounts with balance of 50 for (int x = 0; x<10;x++) { Account [x] = new Account (); Account [x].setId (x); Account [x].setBalance(50); } // Start the welcome message int p; System.out.println("Please enter your ID number\n"); p =input1.nextInt(); // If id is incorrect print message or start mainMenu if (p > Account.length) { System.out.println ("Please re-enter your ID number\n"); } else { a.mainMenu(); } } }
- 01-09-2012, 06:11 PM #12
Re: Error ' Cannot find symbol class UserAccount"
Try debugging your code by printing out the user's input when it is first read in and then again when the code that uses that input is trying to access the array element. Is the value the user input being lost?the users input is not showing the balance of the array element just created
- 01-09-2012, 06:20 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Error ' Cannot find symbol class UserAccount"
You've got an Account in accountAR which is what is being used by the mainMenu stuff to display balances.
This has no balance.
This is not one of the Accounts in the array created in the main() method.
- 01-09-2012, 06:40 PM #14
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Error ' Cannot find symbol class UserAccount"
So this account I have in accountAR is showing the balance? Should I add the array of accounts to accountAR instead of main?
I tried this before but got an error telling me to add ']' but I had
Java Code:Account[] acc = new Account [10]; acc [0] = new Account ();
Last edited by r1b; 01-09-2012 at 06:52 PM.
- 01-09-2012, 06:42 PM #15
Re: Error ' Cannot find symbol class UserAccount"
Is account a subclass of Account?
Case is VERY important.
- 01-09-2012, 06:53 PM #16
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
- 01-09-2012, 06:57 PM #17
Re: Error ' Cannot find symbol class UserAccount"
If you defined your array of Account objects outside of the main() method, then all the methods in the class could see and use it. You can still assign values to the elements of the array inside the main method.
- 01-09-2012, 07:56 PM #18
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Error ' Cannot find symbol class UserAccount"
I am getting this error now for changing the code to this.
']' expectedJava Code:import java.util.*; class accountAR { Account[] Accounts = new Account [10]; Accounts [0] = new Account(); Scanner input = new Scanner(System.in);
';' expected
illegal start of type
<identifier> expected
';' expected
illegal start of type
<identifier> expected
all on line 7 which is Accounts [0] = new Account();
- 01-09-2012, 08:00 PM #19
Re: Error ' Cannot find symbol class UserAccount"
Line 7 belongs in a method.
- 01-09-2012, 08:12 PM #20
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
"Cannot find symbol" error
By MBD in forum New To JavaReplies: 5Last Post: 09-27-2011, 02:41 PM -
"cannot find symbol" error
By droidus in forum New To JavaReplies: 2Last Post: 09-07-2011, 07:43 AM -
Do you know why I'm getting "cannot find symbol" error?
By basla in forum New To JavaReplies: 5Last Post: 04-24-2011, 03:21 PM -
Error "can not find symbol variable"
By FullMetalHollow in forum New To JavaReplies: 5Last Post: 10-04-2009, 09:51 PM -
"Cannont find symbol Constructor" error
By Welsh in forum New To JavaReplies: 7Last Post: 01-25-2008, 12:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks