CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Help understanding toString() (http://www.codingforums.com/showthread.php?t=209467)

Da1dmoney 11-17-2010 05:40 AM

Help understanding toString()
 
I am trying to get out put which looks like this:
Code:

transaction# 0  Error: Tue Nov 16 16:47:15 EST 2010 bye : AMOUNT : 300.0
But this is what I get:
Code:

bye
transaction# 0  Error: Tue Nov 16 16:47:15 EST 2010 null : AMOUNT : 300.0

The code i use to implement this is written as such:
Code:

  public String errorDescription() {                       
 System.out.println("bye");
}                                 
public String toString()          {                                           
return "Error: " + today + " "+ errorDescription() +" : AMOUNT : " + amount;                                                  }

its called here:
Code:

transactions[numTransactions] = new TransferTransaction(accountNumber, accountNumber, amt, balance);                            incrementTransactions();
Print out code:
Code:

for (int i=0;i<numTransactions;i++) {                                       
System.out.println("transaction# " + i + "  " +transactions[i].toString());                               
}

To better understand what i am trying to do. I am trying to print out the word bye in the correct order but instead it prints out on top and in its place a null is printed.

pigpen 11-17-2010 07:44 AM

In your errorDescription() function, just simply add the word "bye" inside the double quotes of your return string

Code:

return "Error: " + today + " "+ errorDescription() +" bye : AMOUNT : " + amount;
I added the "bye" text inside the double quotes containing " : AMOUNT : " so it looks like " bye : AMOUNT : ".

You can get rid of the System.out.println line and the toString() line. You don't need that.

All you are doing is concatenating literal strings and variables via the "+" operator, so you can type whatever inside the double quotes and they'll show up.

Da1dmoney 11-17-2010 09:00 AM

Yes thanks I know this thanks. But what I am actually trying to do is have it call upon 2 different things within the method. for instance
if(balance > 0){
system.out.println("bye");
}
else if(balance <60){
system.out.printl("hi);

so I am unable to just put "bye" within there. This class is being called by another class that is why to string is used.

pigpen 11-18-2010 09:29 AM

Just make a local string variable and set the value of the variable inside your "if" conditional statement.

AND, in your return statement, add the variable, which will display the value that was set by your "if" conditional statement.

Code:

// make a string variable "msg"
String msg = "";

if(balance > 0){
  // set msg value to "bye"
  msg = "bye";
}
else if(balance <60){
  // set msg value to "bye"
  msg = "hi";
}
// return your string with the "msg" variable in the return object
return "Error: " + today + " "+ errorDescription() +" msg : AMOUNT : " + amount;

I don't know how you have your code set up so, if your return string is in another method/function, you may need to pass your "msg" value as a parameter between classes.


All times are GMT +1. The time now is 07:51 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.