Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Java Class : How do I set up a toString method for arrays?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Class : How do I set up a toString method for arrays?

    I'm having a problem with the toString method, or my code could be wrong. Heres an example:
     public class TestClass
    {
      private TestClass[] a;
      private int[] b;
     
      public TestClass(int...b) // Constructor
      {
       this.b
      {
       public TestClass[] concat(TestClass...b1)
      {
         TestClass[] temp = new TestClass[b1.length+b.length]
         for(int i = 0;i<=temp.length;i++)
          return TestClass(temp[i]);  //Trying to return the elements of two arrays put together, I think this could be wrong
    }

    How would I do my toString?
    I want to be able to do something like this :
    (main)
    TestClass a1 = new TestClass[1,2,3,4];
    TestClass a2 = new TestClass[5,6,7,8,9];
    TestClass a3;  // Need help on this also, how do I overload my constructor to get an empty set of array?
    a3 = a1.concat(a2);
    System.out.print(a3); //Want the results " 1,2,3,4,5,6,7,8,9,10 "


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    I'll walk you through a simple example of how to override the toString() method:

    Let's say we have class named A, which had an int variable var which was set in the constructor:
    public class A {
         int var = 0;
     
         public A(int n) {
              var = n;
         }
    }

    Now let's say we want to override A's toString() method to print the value of var 5 times. Our new class would look like:
    public class A {
         int var = 0;
     
         public A(int n) {
              var = n;
         }
         public String toString() {
              String ret = var+"";
              ret = ret+var;
              ret = ret+var;
              ret = ret+var;
              ret = ret+var;
              return ret;
         }
    }

    Now, if we create a main that creates an A Object, and prints out an A Object to the console, our main would look like this:
    public class someClass {
         public static void main(String[] args) {
              A aObj = new A(5);
              System.out.println(aObj);
         }
    }
    and our output would look this like:
    55555
    Now, that is how to override a toString() method. But what about printing out an array? Well, we can call a println on an array. Let's say we had a main which had an int array with the numbers 1 through 5. Then let's print out the array, both by just calling it, and with each int on its own line:
    public class someClass {
         public static void main(String[] args) {
              int[] array = new int[]{1,2,3,4,5};
              System.out.println(array);
              for(int i=0;i<array.length;i++)
                   System.out.println(array[i]);
         }
    }
    And our output would be:
    [1, 2, 3, 4, 5]
    1
    2
    3
    4
    5
    With all of that information (and your knowledge of looping through an array which I see you have), see if you can figure some of it out. If you have some trouble, feel free to ask.
    Last edited by aussiemcgr; May 11th, 2012 at 02:29 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    red7 (May 11th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    a3 = a1.concat(a2)
    For that to work, concat() needs to return a TestClass object.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    @ aussiemcgr, much thanks, understand the toString now.

    @Norm
    I just notice examples in my book about that.

    So now I have a little problem understanding the constructor. How would I set it up?

    private int[] aList = new int[200]; //instance v
     
    public TestClass(int[] a) // constructor
    {
    	aList = a;
    }
    Would this allow to to create an object that takes in a list of array .
    ex. :
    TestClass t = new TestClass(1,2,3,4,5);
    First time making a class while dealing with arrays, so I'm kind of loss.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    Have you coded the class and compiled it? What happened?
    Have you tried the code in post#1?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    Wont let me compile :
      public TestClass[] concat(TestClass...b1)
      {
         TestClass[] temp = new TestClass[b1.length+b.length]
         for(int i = 0;i<=temp.length;i++)
          return TestClass(temp);

    Wont let me grab the length of b1, so i figured something is wrong with my constructor.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    Please post the full text of the compiler's error message.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    length cannot be resolved or is not a field

    at TestClass.concat(TestClass.java:41)
    at TestClassDriver.main(TestClassDriver.java:8)"



    at TestClass.concat(TestClass.java:41) is :
    TestClass[] temp = new TestClass[b1.length+b.length]
    at TestClassDriver.main(TestClassDriver.java:8) is :
    a3 = a1.concat(a2)

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    What you posted is an execution time error, not a compiler error. There is no stack trace for compiler errors.

    Can you post the full text of the compiler error message? The error messages I get include the source line and a ^ under where the error is located:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    Hmm. Using eclipse, and on the line with the x it just says : "length cannot be resolved or is not a field"

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Class : How do I set up a toString method for arrays?

    Sorry, you need to use the javac compiler to get good compiler error messages. The error messages I get compiling your code have nothing to do with the length attribute.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. toString method
    By pelane in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 01:42 AM
  2. Replies: 1
    Last Post: February 12th, 2012, 01:01 AM
  3. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  4. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM