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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -