Results 1 to 7 of 7
- 11-07-2012, 11:51 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Java Error: '____ has private access in ____'
Hello Everyone, I have been working on an assignment, and ran into this issue:
Java Code:symbol: variable nextInt location: variable scanner of type Scanner assignment5-16.java:29: error: newcard(int) has private access in MealCard student.newcard(amount);
Im getting this error everytime i try and call something from within the MealCard class.
the Mealcard class:
and the main class:Java Code:public class MealCard { int bal; public void Mealcard(){ //placeholder } public void newcard(int amount) { if (amount >= 100) { bal = 100; } else { bal = amount; } System.out.println("Your balance is " + bal); } public void addpoints(int amount) { if (amount >= 1) { bal = bal + amount; System.out.println("your new balance is " + bal + " points"); } else { System.out.println("Invalide Amount"); } } public void purchase(int amount) { if ((bal-amount)<0) { System.out.println("You don't have enough points for this item"); } else { bal = bal-amount; } } public int getBal() { return bal; } }
Java Code:class Assignment3_14 { public static void main(String[] args) { int amount, input, balance; MealCard student; Scanner scanner = new Scanner(System.in); student = new MealCard(); System.out.println("Welcome, what do you want to do?"); System.out.println("1. new user"); System.out.println("2. purchase more points"); System.out.println("3. use points"); System.out.println("4. see balance"); System.out.println("make your selection, then hit enter"); input = scanner.nextInt(); switch (input) { case 1: System.out.println("How many points do you want to begin with?"); amount = scanner.nextInt; student.newcard(amount); break; case 2: System.out.println("How many points do you want do add?"); amount = scanner.nextInt; student.addpoints(amount); break; case 3: System.out.println("How many points do you want do spend?"); amount = scanner.nextInt; student.purchase(amount); case 4: balance = student.getBal(); System.out.println("You have " + balance + "points in your account"); break; default: System.out.println("Invalid selection"); break; } } }Last edited by Josh Fedman; 11-08-2012 at 07:29 AM.
- 11-08-2012, 07:12 AM #2
Re: Java Error: '____ has private access in ____'
The error is saying that the newcard method is private. The code you posted has the newcard method as public. Therefore the code and the error message do not match. Make sure you are working with the correct version of your code. Make sure you have saved your code. Make sure you compile the correct code. One way to make sure is to delete the .class file. This forces the compiler to compile the class again.
- 11-08-2012, 07:27 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Re: Java Error: '____ has private access in ____'
Ok, I did what you said, and it cleared up the original error i posted, but another error i was getting is still there.
also, i forgot to mention that the MealCard class has been put into a user-defined package. i dont know if that makes a difference or not...Java Code:symbol: variable nextInt location: variable scanner of type Scanner assignment5-16.java:32: error: cannot find symbol amount = scanner.nextInt;
- 11-08-2012, 07:36 AM #4
Re: Java Error: '____ has private access in ____'
Notice how the error says it cannot find the "variable" nextInt!
- 11-08-2012, 07:45 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Re: Java Error: '____ has private access in ____'
SUPER EDIT: I AM DUMB.
my roommate walked in and figured it out in like 10 seconds. I was missing the parenthesis after .nextInt, so java thought it was a function.
Thanks for your help junky.Last edited by Josh Fedman; 11-08-2012 at 07:54 AM.
- 11-08-2012, 07:52 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Error: '____ has private access in ____'
In this code snippet:
you're using symbol nextInt as if it were a variable; it isn't, it's a method.Java Code:amount = scanner.nextInt;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-08-2012, 07:54 AM #7
Similar Threads
-
How do you access private instance variables from another class?
By javaa in forum New To JavaReplies: 4Last Post: 08-26-2012, 01:09 AM -
has private access in error
By Big-D in forum New To JavaReplies: 1Last Post: 04-06-2012, 02:53 PM -
Default / package-private access
By genkuro in forum New To JavaReplies: 1Last Post: 12-30-2010, 06:08 AM -
has private access error message
By ibrahimyoussof in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 04-13-2010, 04:01 PM -
How to access private data types from public classes?
By kevzspeare in forum New To JavaReplies: 3Last Post: 03-07-2009, 04:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks