my mind must be in a blank, i cant make sense of this

hi

i have a method that gets information from methods in its own class, 'builds' a string and displays the string when the method is called from another class, however the displaying bit isnt happening when i call the method, can some one see y? ill post the different ways i have tried the method:

//tried this....

public String toString ()

{

//method to create a string for displaying client info

String display;

display ="name: "+getName()+"\n"+

"Resident: "+getResident()+"\n"+

"Gross Salary (per week):%.2f %n "+getWeeklyGrossSalary()+"\n"+

"Net Salary (per week):%.2f %n "+getWeeklyNetSalary()+"\n"+

"tax (per week):%.2f %n "+getWeeklyTax()+"\n"+

"medicare (per week):%.2f %n "+getWeeklyMedicare()+"\n"+

"Expenses:%.2f %n "+getWeeklyExpenses();

return System.out.print(display);

}

//and this....

public String toString ()

{

//method to create a string for displaying client info

String display;

display ="name: "+getName()+"\n"+

"Resident: "+getResident()+"\n"+

"Gross Salary (per week):%.2f %n "+getWeeklyGrossSalary()+"\n"+

"Net Salary (per week):%.2f %n "+getWeeklyNetSalary()+"\n"+

"tax (per week):%.2f %n "+getWeeklyTax()+"\n"+

"medicare (per week):%.2f %n "+getWeeklyMedicare()+"\n"+

"Expenses:%.2f %n "+getWeeklyExpenses();

return display;

}

// am calling the method like this...

c[poss].toString();//<--'c' is an array of 'client' objects

much cheers for any assistance

Marc

[2688 byte] By [Aussie_Nerda] at [2007-11-27 5:51:17]
# 1

You have kinda taken half of this and half of that and jammed them together. You don't have to call the toString method, just try and print the Object and somewhere down the chain of method calls it will call the toString method of your Object.

class MyObject {

public String toString() {

return "Hello";

}

public static void main(String[] args) {

MyObject o = new MyObject();

System.out.println(o);

}

}

floundera at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...
# 2
sorry flounder, confusion reigns after reading what u said, forgive me
Aussie_Nerda at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...
# 3

The first toString() looks like thispublic String toString()

{

// etc

return System.out.print(/*whatever*/);

}

This won't compile because PrintStream's print() method is void, so there is nothing to return. If a method is declared to return a String, then it must return a String.

The second is more promising:public String toString()

{

String display;

display = ...

return display;

}

// elsewhere...

c[poss].toString();

The problem here is that you call toString(), it returns a String and you do nothing with the returned string. You say the method displays the string when called, but there is nothing like this in the second method.

Usually the toString() method just returns a String. This makes sense as the caller may want to display it, or may want to do something else like save it to a log file. The second form looks better but it should be used likeSystem.out.println(c[poss].toString());

Or, as flounder suggests, just System.out.println(c[poss]);

pbrockway2a at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...
# 4

thanx heaps! its displaying now, however i have one more question, when calling the method, its currently like so:

System.out.println(c[poss].toString());

which works no prob, BUT for the methods that return a number there is no format involved and hence the number will be like 151.16247389 etc, i tried

System.out.printf("%.2f %n",c[poss].toString());

however as ull probly tell, this doesnt work, do i format in the call of the method or the string in the mthod itsself? and whats the best way to do the format?

Cheers and regards

Aussie_Nerda at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...
# 5
Do the formatting inside the toString() method."Gross Salary (per week):" + String.format("%.2f"+getWeeklyGrossSalary())+"\n"It might be a good idea to use a StringBuilder or something to keep it all
pbrockway2a at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...
# 6
thanx so much for that, all working fine now, but i just haf to ask for my own curiosity, what did u mean by a StringBuilder? never come across em before (im a noob)
Aussie_Nerda at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...
# 7

System.out.println(c[poss].toString());

Once again, you don't need the call to toString. If you do you are making an unecessary method call. As I said before a call to println makes a call to another method etc etc and the last call is to your toString method. Adding toString in the print statement the method returns a String so now it goes down the method call chain and ends up calling the toString method of String class which in returns itself. Making it unnecessary.

floundera at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...
# 8

> what did u mean by a StringBuilder?

StringBuilder buf = new StringBuilder();

buf.append("Name: ").append(getName()).append("\n");

buf.append("Resident: ").append(getResident()).append("\n");

buf.append("Net Salary (per week): ").append(String.format("%.2f%n"), getWeeklyTax());

// etc

return buf.toString();

pbrockway2a at 2007-7-12 15:40:13 > top of Java-index,Java Essentials,New To Java...