0

I have a toString() method that goes through a for loop and is supposed to print each element in the array, but I'm still getting the location when I call the toString() method. How could I fix this?

public void toString(String[] num) {        
    for (int index = 0; index < num.length; index++) {
        System.out.println(num[index]);
    }
}

public static void main(String[] args) {
    //test negative number, output should be -10000000
    int largeNum1[] = {-2, 0, 0, 0, 0, 0, 0, 0};
    int largeNum2[] = {1, 0, 0, 0, 0, 0, 0, 0};
    int numSize1 = (largeNum1.length + largeNum2.length) + 1;
    int[] sum1 = new int[numSize1];
    int[] answer1 = sumNumbers(largeNum1, largeNum2, sum1);
    System.out.println(largeNum1.toString() + " + " + largeNum2.toString() + " = " + answer1.toString());
}

Instead of -20000000 + 10000000 = -10000000, my output is [I@182decdb + [I@26f0a63f = [I@4361bd48

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
krk
  • 23
  • 5
  • 1
    Your method wouldn't print "-20000000" either, it would print "-2\n0\n0\n0\n0\n0\n0\n0\n". – Tom Mar 07 '21 at 03:04

3 Answers3

2

Change array.toString() to toString(array). Also your method needs to be static because main method is static. And if you want to use it in print statement, then it should return something. That's why I have changed the return type of your method to String:

public static String toString(int[] num) { 
    String s = "";       
    for (int index = 0; index < num.length; index++) {
        s += num[index];
    }
    return s;
}

public static void main(String[] args) {
    //test negative number, output should be -10000000
    int largeNum1[] = {-2, 0, 0, 0, 0, 0, 0, 0};
    int largeNum2[] = {1, 0, 0, 0, 0, 0, 0, 0};
    int numSize1 = (largeNum1.length + largeNum2.length) + 1;
    int[] sum1 = new int[numSize1];
    int[] answer1 = sumNumbers(largeNum1, largeNum2, sum1);
    System.out.println((toString(largeNum1)) + " + " + (toString(largeNum2)) + " = " + (toString(answer1)));
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Haseeb Ahmad
  • 118
  • 1
  • 13
1

Change largeNum1.toString() to toString(largeNum1) to call your method instead of the built-in Object.toString().

You'll also need to modify your method to take int[] rather than String[].

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

You want to use .toString() method. In java every class is child of Object class either directly or indirectly. Object class contains toString() method. We can use toString() method to get string representation of an object.

If we did not define toString() method in your class then Object class toString() method is invoked, otherwise our implemented/Overridden toString() method will be called.

Now if your goal is to override toString then you need to modify your code.

public class ToStringDemo {
    
    int[] largeNum1 ;
    int[] largeNum2;
    int numSize1;
    int[] sum1;
    int[] answer1;
    
    @Override
    public String toString() {
        return ToStringDemo.toString(largeNum1) + " + " + ToStringDemo.toString(largeNum2) + " = " + ToStringDemo.toString(answer1);
    }
    public static String toString(int[] num) {
        StringBuilder stringBuilder = new StringBuilder();
            
        for (int index = 0; index < num.length; index++) {
            stringBuilder.append(num[index]);
        }
        
        return stringBuilder.toString();
    }
    
    

    public static void main(String[] args) {
        //test negative number, output should be -10000000
        ToStringDemo toStringDemo = new ToStringDemo();
        toStringDemo.largeNum1 = new int[]{-2, 0, 0, 0, 0, 0, 0, 0};
        toStringDemo.largeNum2 = new int[]{1, 0, 0, 0, 0, 0, 0, 0};
        toStringDemo.numSize1 = (toStringDemo.largeNum1.length + toStringDemo.largeNum2.length) + 1;
        toStringDemo.sum1 = new int[toStringDemo.numSize1];
        toStringDemo.answer1 = toStringDemo.sumNumbers(toStringDemo.largeNum1, toStringDemo.largeNum2, toStringDemo.sum1);
        
        System.out.println(toStringDemo);
    }

    private int[] sumNumbers(int[] largeNum12, int[] largeNum22, int[] sum1) {
        //Your sum logic here, return your result array
        return null;
    }

}
Md Kawser Habib
  • 1,966
  • 2
  • 10
  • 25