Question:
help with java toString() please?
anonymous
2010-03-16 13:21:46 UTC
hello here is my class file and I am trying to get it to compile, but it wont accept the variable 'leap' in the toString(). Can anyone help me?


//

public class Dog {

private String name;
private int years;

public Dog(String title, int age){
name = title;
years = age;
}

public int setAge(int age)
{
return years = age;
}

public String getName()
{
return name;
}

public String setName(String name)
{
return name;

}

public int personYears()
{
int leap = 7 + years;

return leap;
}

public String toString()
{
String description = ("The dog is " + years + " years old, and is named - " + title +" . "+ "\n" +
"In human years the dog is " + leap);
return description;
}
}


//
Four answers:
anonymous
2010-03-16 13:34:33 UTC
first of all leap is initalized in a method therefore leap will be not recognized. you must initialize leap outside the method personYears, moreover you have to call name not title because title is a variable inside the constructor dog while name is in the class. Moreover the string description is also initialized from the method tostring you can either use return the dog is etc or you initialize string description = ""; outside the method toString();
gregegg10
2010-03-16 13:24:48 UTC
I believe you need to define Leap up above at the beginning, outside of the personYears() function.



So, up top:

public String leap;
Beef_stew4u
2010-03-16 13:52:43 UTC
try this line to convert an int into a string



String myString = Integer.toString(leap);



It will take your leap int, and convert it to a new string called myString. You can change myString to whatever you want. Just replace your leap variable in String description to whatever you name your myString.
?
2016-12-12 18:13:20 UTC
For the toString() to artwork properly, as you may assume, you are able to desire to alter this: public String toString(int fee, int greater healthful) to public String toString() yet now you will prefer some issues. a million. you're calling the toString approach from substantial. this would possibly not artwork as substantial is static and toString isn't. carry at the same time errors. 2. you will no longer have get right of entry to to the siut or fee variables interior the toString approach as they're defined interior the biggest approach. suggestion on the thank you to repair this. Create a constructor for the cardboard classification. public Card(int theValue, char theSuit) { fee = theValue; greater healthful = theSuit; } Make inner maximum istance variables for fee and greater healthful interior the cardboard classification. you are able to save what you have interior the biggest approach. inner maximum int fee; inner maximum char greater healthful; Now, interior the biggest approach, formerly this line: card = toString(); Have this. Card aCard = new Card(fee, s); substitute the line: card = toString() ; to this: card = aCard.toString(); One final ingredient, the way you're inquiring for the greater healthful, you're inquiring for a letter. interior the code following that, you have become an int, no longer a char. Then interior the if statements, you're checking for ints no longer chars. So i could substitute this line: device.out.println("enter greater healthful Letter: "); to a minimum of something like: device.out.println("enter greater healthful (0 for:golf equipment, a million for diamonds, 2 for hearts, 3 for spades): "); Plus you are able to desire to alter this code: else if (greater healthful == 3) { cardsuit = "hearts"; } to else if (greater healthful == 2) { cardsuit = "hearts"; } as greater healthful == 3 is for spades no longer hearts. Hearts is two in accordance to what you have. good success with it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...