Results 1 to 10 of 10

Thread: toString Method

  1. #1
    hndlz is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default toString Method

    my method isnt printing the correct values.

    Java Code:
     
         public String toString()
       {
          // headings
          String out =  " Principal   Maturity   Interest   Compounded" 
                  + "   Accval\n" + " =========   ========"
                  + "   =========  ==========   ======" + "\n";
          //each CD object on list
          for (int i = 0; i < list.size(); i++)
          {
                    CD money = list.get(i);
                    
                    CD mature = list.get(i);
                    
                    CD rate = list.get(i) ;
                    
                    CD comp = list.get(i) ;
                    
                    out = out + money + mature 
                    + rate + "\n";
               
                    
          }
          return out + "\n" ;
          
       }


    it prints this...


    Principal Maturity Interest Compounded Accval
    ========= ======== ========= ========== ======
    CD@106082CD@106082CD@106082
    CD@1301ed8CD@1301ed8CD@1301ed8
    CD@3901c6CD@3901c6CD@3901c6
    CD@a37368CD@a37368CD@a37368
    CD@edc3a2CD@edc3a2CD@edc3a2
    CD@1c6f579CD@1c6f579CD@1c6f579

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: toString Method

    What should it print?

    And, eg, what is money supposed to be? At the moment you say it is a CD instance (and assign it one from the list), but shouln't it be a money amount?

  3. #3
    hndlz is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: toString Method

    it should print the data from a text file. money is the principal.

  4. #4
    hndlz is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: toString Method

    and here is the main method. i dont really think there are any problems here though...

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class CDDriver {
        public static void main(String [] args) throws IOException
        {
            
            CDList list = new CDList();
            
            Scanner cdFile = new Scanner (new File("CD-data.txt")) ; 
            
          // for(int year = 1; year <=10;year++) 
            
            while(cdFile.hasNext())          // while not end eof
            {
                String row = cdFile.nextLine() ;
                System.out.println(row) ;
                
                Scanner round = new Scanner (row) ;
                
                int Principal = round.nextInt() ;
                int Maturity = round.nextInt() ;
                double Interest = round.nextDouble() ;
                String Compound = round.next() ;
               
               
                  
           
            CD one = new CD(Principal, Maturity, Interest, Compound) ;
            list.addCD(one);
                       
            }        
      
           
             
           System.out.println( "\n" + list.toString());
            
                    
        }
        
    }

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: toString Method

    It would help if we could see the rest of the class. We don't know what list is. Also, does list.get(i) return a different CD instance each time it is called...that's where know what list is would help.

    What is CD? Can you do a + operation against it? Is it an arithmetic type like int?

    I know exactly what the problem is, I'm just trying to get you to think what you are doing.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: toString Method

    If money is supposed to be the principal then I would expect it to be declared as double not CD. Yes, the compiler will then (helpfully) complain.

    -----

    Get the CD from the list, then declare money and assign it that CD's principal, etc. At that point you will have something nice to print.

  7. #7
    hndlz is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: toString Method

    jlczuk, list is arraylist of CD objects

    pbrockway2 im not sure if that is what im supposed to do. it tells me its incompatible types.
    ok so here's my list class

    Java Code:
     import java.util.ArrayList;
    
    /**
     * A class to implement a list of CD objects. 
     */
    public class CDList
    {
        //instance variables
        private ArrayList<CD> list ;
        
       /**
        * Creates an empty list.
        */ 
       public CDList()
       {
          
           list = new ArrayList<CD>() ;
       
       }
       /**
        * Adds a CD object to the list.
        * @param current 
        */
        public void addCD(CD current) 
        {
        
            list.add(current);
    
        }
        
         public String toString()
       {
          // headings
          String out =  " Principal   Maturity   Interest   Compounded" 
                  + "   Accval\n" + " =========   ========"
                  + "   =========  ==========   ======" + "\n";
          //each CD object on list
          for (int i = 0; i < list.size(); i++)
          {
                    CD money = list.get(i);
                    
                    CD mature = list.get(i);
                    
                    CD rate = list.get(i) ;
                    
                    CD comp = list.get(i) ;
                    
                    out = out + money  + mature 
                    + rate + comp + "\n";
               
                    
          }
          return out + "\n" ;
          
       }
         
    }
    Last edited by hndlz; 04-20-2012 at 02:50 AM.

  8. #8
    hndlz is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: toString Method

    ok so after some fiddling around (talking to a classmate), i got it to work.
    i had to fix the toString method like this


    Java Code:
     public String toString()
       {
          // headings
          String out =  " Principal   Maturity   Interest   Compounded" 
                  + "   Accval\n" + " =========   ========"
                  + "   =========  ==========   ======" + "\n";
          //each CD object on list
          for (int i = 0; i < list.size(); i++)
          {
                    CD one = list.get(i);
                    
                    int principal = one.getPrincipal();
                    
                    int maturity = one.getMaturity() ;
                    
                    double interest = one.getInterest() ;
                    
                    String compound = one.getCompound() ;
                    
              out = out + Align.right(principal, 12) + Align.right(maturity, 16)
               + Align.right(interest, 18) + Align.right(compound, 24) + "\n" ;
               
                    
          }
          return out + "\n" ;
          
       }
         
    }

  9. #9
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: toString Method

    That looks MUCH better! Nicely done.

    You could also have used this alternative form of for loop:
    Java Code:
    for (CD one : list)  
    {
        int principal = one.getPrincipal(); 
       ...your stuff here ...
    }

  10. #10
    hndlz is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: toString Method

    yeah, it was the last class of the semester today and our prof tells us today. had no clue about it before this afternoon, but ill stick to what i know for now or else ill confuse myself =p
    Last edited by hndlz; 04-20-2012 at 04:10 AM.

Similar Threads

  1. Override toString() Method
    By Apparition78 in forum New To Java
    Replies: 1
    Last Post: 02-03-2012, 10:32 PM
  2. URGENT toString method help
    By johnnyjava in forum New To Java
    Replies: 17
    Last Post: 08-16-2011, 08:09 PM
  3. Why isn't my toString method working
    By arri3oi in forum New To Java
    Replies: 7
    Last Post: 12-13-2010, 05:34 AM
  4. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 11:43 PM
  5. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 04:08 AM

Posting Permissions

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