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 4 of 4

Thread: Array toString

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array toString

    I got this to compile but in the interaction pane im trying to make it look like the picture below but i come out with 0, 1 , 2, 55.8. Thanks

    lab5unittest1.gif

    HTML Code:
       public class TestScores
       {
          private double[] scores;
       
          public TestScores(double [] newScores)
          {
             scores = new double[newScores.length];
             for (int i = 0; i < newScores.length; i++)
             {
                scores[i] = newScores[i];
             }
          }
          public double getAverage()
          {
             double average = 0.0;
             double total = 0.0;
          
             for (double val : scores)
             {
                total += val;
             }
          
             average = total / scores.length;
          	
             return average;
          }
        
          public String toString(){
             String str = "";
          
             for(int i = 0; i < scores.length; i++)
             {
                str += i + " ";
             }
          
             str += scores[scores.length];
             return str;
          }	 
         
          
       }
       
       


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Array toString

    Use a for loop to increment a variable, this variable is to be used as an index for accessing the array elements, append the array element to a string, loop. All i can say without giving code...

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

    smithmar (July 23rd, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Array toString

    Quote Originally Posted by smithmar View Post
    I got this to compile but in the interaction pane im trying to make it look like the picture below but i come out with 0, 1 , 2, 55.8.

    Really? I came out with "Exception in evaluation thread java.lang.ArrayIndexOutOfBoundsException: 3"

    Instead of playing with the interactive mode in jGRASP, here's my suggestion:

    Make a file with a public class that has a public static void main() function that creates the array and instantiates a TestScores object and tries the toString() method.

    Compile and execute the code and see how it goes. If there are problems that you don't understand, then post the complete code and post the exact message(s) Don't paraphrase.

    That way, everyone can be working on exactly the same thing that you are doing and maybe someone can offer meaningful help that directly applies to the actual code that you are using.

    [/begin Editorial]
    I mean, I know that all of the books seem to recommend some kind of IDE for beginners, but I have found it extremely frustrating to try to help people understand Java fundamentals when the IDE keeps getting in the way. (Especially when I am not in the same room, looking over their shoulder and making sure that they click the right "intuitive" icon or follow an "intuitive" menu sequence.) People end up learning all kinds of stuff about a particular IDE instead of concentrating on learning Java.
    [/end Editorial]


    Compiling with command line javac and executing with command line java is kind of a "least common denominator" that every one can relate to and can, maybe, reproduce your exact problem.

    After people have gained a thorough knowledge of fundamentals, the right IDE can make management of large projects somewhat easier to organize, and then I say "Go for it!" They won't be needing to ask me for help anyhow...


    Cheers!

    Z
    Last edited by Zaphod_b; July 23rd, 2012 at 05:23 PM.

  5. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array toString

    Thanks guys, i figured it out. Had to change my String str line to str += scores[i] + " ";

Similar Threads

  1. Can I use toString() here?
    By titowinky in forum Java Theory & Questions
    Replies: 2
    Last Post: May 30th, 2012, 07:15 AM
  2. toString method
    By pelane in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 01:42 AM
  3. Why cant I use toString() with vector?
    By tarkal in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 08:24 AM
  4. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 05:04 PM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM