java - Basic programming class...first time using a string. -
i supposed make program incorporates methods , string format. situation user inputs new tree example: tree t = new tree(27, 43.25, "pine") , type t.describe() , receive output "tree number 27 has circumference of 43.25 , of species pine."
here code:
public class tree{ int serial; double circumference; string species; tree(int serial, double circumference, string species) { this.serial = serial; this.circumference = circumference; this.species = species; } public string describe() { string.format("tree number %d has circumference of %.2f , of species %s.", serial, circumference, species); return describe(); } }
the program blows though. help!
the problem calling describe()
within describe()
, nothing stopping calling ad infinitum. must getting stackoverflowerror
.
the solution here simple - string.format
returns string
want. return it.
return string.format("tree number %d has circumference of %.2f , of species %s.", serial, circumference, species);
also, not call describe()
describe()
. that's made "blow up".
Comments
Post a Comment