Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-17-2010, 05:40 AM   PM User | #1
Da1dmoney
New to the CF scene

 
Join Date: Nov 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Da1dmoney is an unknown quantity at this point
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.
Da1dmoney is offline   Reply With Quote
Old 11-17-2010, 07:44 AM   PM User | #2
pigpen
Regular Coder

 
Join Date: Dec 2007
Posts: 137
Thanks: 1
Thanked 21 Times in 21 Posts
pigpen is on a distinguished road
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.
pigpen is offline   Reply With Quote
Old 11-17-2010, 09:00 AM   PM User | #3
Da1dmoney
New to the CF scene

 
Join Date: Nov 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Da1dmoney is an unknown quantity at this point
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.
Da1dmoney is offline   Reply With Quote
Old 11-18-2010, 09:29 AM   PM User | #4
pigpen
Regular Coder

 
Join Date: Dec 2007
Posts: 137
Thanks: 1
Thanked 21 Times in 21 Posts
pigpen is on a distinguished road
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.
pigpen is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 PM.


Advertisement
Log in to turn off these ads.