This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

int cannot be dereferenced?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umm this is my first post but my friends have told me to use this site if i have any problems with java. this is my first programming class and i plan on taking more. i have to make a deal or no deal game for class and i got stuck at trying to shuffle the amount of money in case so that its random and i got the error.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dereferencing means using a reference variable to call a method or access a field of an object instance.
e.g.

Here you are dereferencing the reference variables to call the substring method.

This only works with reference types however. You get that error when you try to dereference a primitive type (like int, long, double, etc).
Primitive types don't have methods or fields, so you can't dereference them.
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your cases variable is an int. It doesn't have a length, it's just a number like 7 or 83.

also...there isn't anything called "Int" in Java. There are "int" (note lower-case 'i'), and there are Integers (with a capital 'I').
 
Bartender
Posts: 3307
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch
 
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I have been back to your code and got rid of the long lines; you can see how it should be done.
A few points which will improve your code.
Use spaces for indenting, not tabs. Get an editor which converts tab to 4 spaces automatically and that will be easy.
Give your variables descriptive names: I can't tell what the array a means.

Maybe you mean to use the name of an array rather than cases in the line with the compiler error.

What do the loops mean? you should go through them very carefully with a pencil and work out whether they will ever run.
 
antonio shaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like to thank every one for there support. i will use your advice to help better my coding
 
antonio shaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the for loop is just to print out the numbers 1-26 for the option of what case they will like to pick.(which i havent gotten around too yet) while the do while loop is to sort the amount of money that is in the cases so that it isnt in a linear order.
 
Campbell Ritchie
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the inner loop (the for) with a pencil and paper and work out what it actually does. The outer loop isn't a while loop, but a do‑while loop (sometimes simply called do loop). That is slightly different.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!