Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •