Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Junky

Thread: Java Error: '____ has private access in ____'

  1. #1
    Josh Fedman is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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:
    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;
        }
    }
    and the main class:
    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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  3. #3
    Josh Fedman is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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.

    Java Code:
      symbol:   variable nextInt
      location: variable scanner of type Scanner
    assignment5-16.java:32: error: cannot find symbol
                amount = scanner.nextInt;
    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...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Java Error: '____ has private access in ____'

    Notice how the error says it cannot find the "variable" nextInt!

  5. #5
    Josh Fedman is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Error: '____ has private access in ____'

    In this code snippet:

    Java Code:
    amount = scanner.nextInt;
    you're using symbol nextInt as if it were a variable; it isn't, it's a method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Java Error: '____ has private access in ____'

    Quote Originally Posted by Josh Fedman View Post
    i have no idea why it thinks its a variable.
    Yes you do if you just take a deep breath and think for a moment.

    In the Scanner class nextInt is not a variable it is a .....? Because it is a .... it needs ....!
    quad64bit likes this.

Similar Threads

  1. Replies: 4
    Last Post: 08-26-2012, 01:09 AM
  2. has private access in error
    By Big-D in forum New To Java
    Replies: 1
    Last Post: 04-06-2012, 02:53 PM
  3. Default / package-private access
    By genkuro in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:08 AM
  4. has private access error message
    By ibrahimyoussof in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 04-13-2010, 04:01 PM
  5. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •