| Author |
New to Java and wanting to learn
|
Kamran Khaen
Greenhorn
Joined: Jan 16, 2010
Posts: 15
|
|
Needed help getting a compiler error
new to java, error is
"int cannot be dereferenced" i know its because im trying to display an int as a string but i know theirs a way just need help
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
age is an int variable, a primitive and you can't call a method on a primitive. Since String is smart enough to figure out what to do here, why not simply do:
|
 |
Kamran Khaen
Greenhorn
Joined: Jan 16, 2010
Posts: 15
|
|
bessically, in my assignment ive been asked to add a toString method, which includes the student id, name , age and points. Now the error is saying im "missing a return statement".
and would this method be ok as a toString method or do i have to carry out different codeing?
|
 |
Siddhesh Deodhar
Ranch Hand
Joined: Mar 05, 2009
Posts: 117
|
|
toString() method looks OK. You need to return your String at last because return type of method is String..
public String toString()
{
String StudentInfo = "Student ID: " + idNumber +
"\nName: " + name +
"\nPoints: " + achievementPts +
"\nAge: " + age;
return StudentInfo;
}
well, Welcome to java ranch..You will enjoy java here
|
Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
Please do not post in all uppercase. Please read this for more information. I have adjusted to topic title for you.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Kamran Khaen
Greenhorn
Joined: Jan 16, 2010
Posts: 15
|
|
Siddhesh Deodhar wrote:toString() method looks OK. You need to return your String at last because return type of method is String..
public String toString()
{
String StudentInfo = "Student ID: " + idNumber +
"\nName: " + name +
"\nPoints: " + achievementPts +
"\nAge: " + age;
return StudentInfo;
}
well, Welcome to java ranch..You will enjoy java here 
fanks it worked, just wondering is the to string method spost to print to terminal window?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
"j Blue", please check your private messages for an important administrative matter.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Moving to Beginning Java. And please use a meaningful subject line next time.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: New to Java and wanting to learn
|
|
|