• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

int cannot be dereferenced

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this is the stuff teachers teach you in high school, I'm not looking forward to it.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28397
100
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic