-1

I've been staring at this program for hours, maybe a second perspective could help.

import java.util.Arrays;
import java.util.Random;

public class Examples

{
private int numbers[]; 
private String result;

     public Examples()
        {
     numbers = new int[10];
         Arrays.fill(numbers, 42);
        } 

        public Examples(int[] array)
        {
        numbers = Arrays.copyOf(array, array.length);
        }


 public void setNumbers (int numbers) 
    {                                           
    this.numbers = new int[numbers];
    }
 public int [] getNumbers()
 {                                      
    return numbers;
 }



public  String toString()
{
    String result = new String();
    int i;                                  
    for (i = 0; i < numbers.length; i++)
        result = numbers + "\t";
        return result;
}

I'm supposed to create a class that stores integer and then prints out a certain output depending on what static void main method I am provided with. The one I am currently on is,

{
   public static void main( String [] args )
   {
      int i;
      FunkyNumbers funNumbers, moreFun;

      System.out.println("FunkyClientA:");
      System.out.println("Test default constructor and toString method");
      System.out.println();

      funNumbers = new FunkyNumbers();
      System.out.println("toString result from funNumbers:");
      System.out.println( funNumbers.toString() );
      System.out.println();

      moreFun = new FunkyNumbers();
      System.out.println("toString result from moreFun:");
      System.out.println( moreFun.toString() );

   } // end of method main

} //

The result is supposed to be Test default constructor and toString method

toString result from funNumbers: 42 42 42 42 42 42 42 42 42 42

toString result from moreFun: 42 42 42 42 42 42 42 42 42 42

however I get, Test default constructor and toString method

toString result from funNumbers: [I@6e1408

toString result from moreFun: [I@e53108

user1804737
  • 65
  • 1
  • 3
  • 9

3 Answers3

2

Issue: You are always assigning a new value to result and not appending.

Better to use StringBuilder as :

    StringBuilder result = new StringBuilder();
    for (int i = 0; i < numbers.length; i++)
        result.append(numbers + "\t");
    return result.toString();

If don't want to use StringBuilder then

    String result = new String();
    for (int i = 0; i < numbers.length; i++)
        result = result + numbers + "\t";//append in result
    return result;

Also make sure the method is available in FunkyNumbers class. If not accessible there, then move/copy there.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

try

        result += numbers[i] + "\t";

in your ToString() method

Include the code to your FunkyNumbers class and i might be able to provide better feedback.

0

An array in java is an object. So the variable funNumbers has as value not the array but a reference which points to the address in your memory where the array is located. When you use toString() you print this memory address on your screen and not the array as a String. You should use a StringBuffer instead to "convert" the array to a String.

private static String convertArrayToString(int[] a) {
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<a.length; i++)
            sb.append(a[i]);
        return sb.toString();
    }

You can also use Arrays.toString(arg) to "convert" your array to a String. As argument you give your array and you get a String representation of your array back. If your array is multidimensional use Arrays.deepToString(arg) instead.

tim_a
  • 940
  • 1
  • 7
  • 20