9 Replies Latest reply: Oct 22, 2006 7:39 PM by 807607 RSS

    Printing Arrays

    807607
      I'm having trouble printing out my arrays. I need a toString() method, but i'm not really sure how they work. My array includes individual numbers and looks somthing like this:

      Option 1: 1 2 3 4 5 // where "1" is the first index of the array
      Option 2: 4 5 6 7 8
      Option 3: 9 10 11 12 13

      How do i create a toString() method to print out the "1 2 3 4 5" ect.?
      Thank you for any help you can give me!
        • 1. Re: Printing Arrays
          807607
          Use a StringBuffer or StringBuilder.
          Loop over the array and add each element (plus a space) to the buffer.
          Return the String from the buffer.
          • 2. Re: Printing Arrays
            807607
            I don't suppose you could give me a small example? lol
            • 3. Re: Printing Arrays
              807607
              declare StringBuffer
              loop {
                  add element to buffer;
                  add space to buffer;
              }
              return buffer string; // see note
              Note: you might also want to call the trim() method to remove the space after the last element.
              • 4. Re: Printing Arrays
                807607
                Since hes a beginner, he might just want to start with looping through the array and calling System.out.print.
                for(int i=0; i<someArray.length; i++)
                {
                     System.out.print(someArray[i] + " ");
                }
                • 5. Re: Printing Arrays
                  807607
                  Yeah, I was going to mention that I didn't think toString() was the way to go for arrays.
                  • 6. Re: Printing Arrays
                    807607
                    I tried that CaptainMorgan but i get the usual "[I@9dfdc3" output when i try to print it. All i really want to do is somthing like ask the user to input 4 numbers, then add each one to the array and print it out.

                    ie.

                    Enter 4 numbers: 1 2 3 4
                    You entered: 1 2 3 4

                    Isn't there a simple way to do this by writing a toString method? Thanks for the help so far!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                    • 7. Re: Printing Arrays
                      807607
                      hmm what "type" do you apply to integers within an array?

                      I tried:

                      int number = myArray[1]

                      but it gave me an "incompatible types" error. How would i go about taking an index from an array and creating a new variable with it?
                      • 8. Re: Printing Arrays
                        807607
                        If you are getting incompatible types then myArray doesn't hold ints.
                        • 9. Re: Printing Arrays
                          807607
                          k nevermind i solved it. I forgot i had a 2 dimensional array lol. Thanks for the help!