*
The moose likes Beginning Java and the fly likes int cannot be dereferenced Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int cannot be dereferenced" Watch "int cannot be dereferenced" New topic
Author

int cannot be dereferenced

Ariana Abrenica
Greenhorn

Joined: Jan 06, 2012
Posts: 12
hi our teacher made this example for random numbers but when I tried to run this code, there were three errors in c=n.nextInt(47)+1;



[Thumbnail for cats.jpg]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Welcome to the Ranch!

On line 6 you declare 4 variables, c, sw, random and n. These all have the same type, int. int is one of the primitive types in Java which means it does not have any methods or fields you can call using the dot operator.

You are calling n.nextInt(47);. There are two methods called nextInt that take a single int value - one in java.util.Random and one in java.util.Scanner. Since in the latter the int is the radix I assume you actually want the former. For that you would need an instance of java.util.Random.
I think that line 6 is actually a mistake; either you copied it incorrectly or your teacher made a mistake. I think it should be this:
And can you please UseCodeTags next time? Make sure to preserve the indentation as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

If this is the stuff teachers teach you in high school, I'm not looking forward to it.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

line 6 is poor style IMO anyway. each declaration should be a separate line. like i said, just my opinion. the variable names are meaningless as well


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
There are other style problems, eg a class called “aa” and use of tabs for indenting. Also binary operators should be separated from their operands by single spaces.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Your teacher couldn't get a job doing what they're teaching. That's awful.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Your teacher couldn't get a job doing what they're teaching. That's awful.

no, that is common. we used to joke about it, but it isn't really funny. if the economy and especially education weren't in such a bind now i could even get a job as a teacher. they don't even require degrees anymore
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ken Blair wrote:Your teacher couldn't get a job doing what they're teaching. That's awful.


There's a whole industry of certification exams which appears (to me) to require people to look at horribly bad code and figure out what it does. On the one hand, figuring out what horribly bad code does is a useful skill. But on the other hand I have this sinking feeling that the people who take those exams might be taking the exam questions as examples to be imitated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int cannot be dereferenced
 
Similar Threads
creating a list of subsets from N elements
here is a question and i have a very similar code but i need to convert it
How to extract the value?
How do i get all subsets?
Chapter 5 K&B -Doubt