• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int cannot be dereferenced

 
Ariana Abrenica
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

cats.jpg
[Thumbnail for cats.jpg]
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jj Hill
Ranch Hand
Posts: 41
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is the stuff teachers teach you in high school, I'm not looking forward to it.
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your teacher couldn't get a job doing what they're teaching. That's awful.
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic