Results 1 to 6 of 6
  1. #1
    teardrop3903 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default help with toString

    How do i get my toString() methods to work properly?

    when i run the program i still get the memory address for options inner class instead of the toString().

    Java Code:
    import java.io.Serializable;
    
    public class OptionSet implements Serializable{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private String name;
    	private int number_Of_Options;
    	private Option[] options;
    
    	public OptionSet (String name, int count) {
    		this.name = name;
    		this.number_Of_Options = count;
    		options = new Option[number_Of_Options];
    	}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public Option[] getOptions() {
    		return options;
    	}
    
    	public void setOptions(Option[] options) {
    		this.options = options;
    	}
    	
    	public Option getOption(String name) {
    		int  i;
    		i = findOption(name);
    		if(i == -1)
    		{
    			System.out.println("Option not Found");
    		}
    		return options[i];
    	}
    	
    	public void setOption(int i, String name, float price)
    	{
    		options[i] = new Option(name, price);
    	}
    	
    	private int findOption(String name) {
    		int i = 0;
    		while(i < number_Of_Options && options[i].getName() != name)
    		{
    			i++;
    		}
    		if (i == number_Of_Options)
    			return -1;
    		else
    			return i;
    	}
    
    	public String toString() {
    		return "OptionSet - " + name + "\n" +
    				number_Of_Options + " options\n" +
    				options;
    	}
    	
     	class Option implements Serializable{
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = 1L;
    		private String name;
    		private float price;
    		
    		public Option (String name, float price) {
    			this.name = name;
    			this.price = price;
    		}
    		
    		public String getName() {
    			return name;
    		}
    		public void setName(String name) {
    			this.name = name;
    		}
    		public float getPrice() {
    			return price;
    		}
    		public void setPrice(float price) {
    			this.price = price;
    		}
    		
    		public String toString() {
    			return "\t" + name + " - $" + price;
    		}
    	}
    }
    Driver:
    Java Code:
    public class Driver {
    	
    	public static void main(String[] args) {
    		OptionSet test = new OptionSet("Color", 2);
    		test.setOption(0, "red", 45);
    		test.setOption(1, "blue", 54);
    		System.out.print(test);
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You aren't printing options, you are printing an option set, you should have a toString in the optionset class which builds a string with a string builder that contains each element in the options array.

  3. #3
    teardrop3903 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    so i have to separate each individual object in the array instead of trying to print out the whole thing?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can't print an array, since the toString isn't defined by you. You can however; had the optionSet class have a toString method which loops through the array and builds a string with the options toString method, it then returns the string. This will allow your print of the optionset to work as you expect.

    Java Code:
    class X{
      Y[] y = new Y[10];
      public String toString(){
        StringBuilder sb = new StringBuilder();
        for(int j = 0; j < y.length; ++j){
          sb.append(y[j].toString());
        }
        return sb.toString();
      }
      class Y{
        int z;
        public String toString(){
          return z + " ";
        }
      }
    }

  5. #5
    teardrop3903 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Thank you for your help. That example was very useful.

    Heres my new toString().
    Java Code:
    public String toString() {
    		StringBuilder sb = new StringBuilder();
    		sb.append("OptionSet - " + name + "\n");
    		sb.append(number_Of_Options + " options\n");
    	    for(int i = 0; i < number_Of_Options; ++i){
    	      sb.append(options[i].toString());
    	    }
    	    return sb.toString();
    	}

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped. Remember that any time you are concatenating a lot of strings it is much more efficient to use a string builders opposed to using the + operator. I believe the + operator way takes about twice as many machine instructions as the append method of a string builder.

    If you are done, please mark your thread solved with the thread tools at the top.

Similar Threads

  1. toString
    By justin23 in forum New To Java
    Replies: 13
    Last Post: 05-02-2010, 02:44 PM
  2. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 08:52 AM
  3. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 11:43 PM
  4. Arrays.toString
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:35 PM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:32 PM

Posting Permissions

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