0

I'm creating a program that uses two methods to print all the odd numbers in an array and then get the sum of the odd numbers. However, I'm getting an output that makes no sense. This is the code that I'm using:

public class ArrayMethods1 {
    public static int[] printOdds(int[]arrayExample) {
        int i;
        String oddNum = "";
        int [] newArray = new int [3];
        int x = 0;
        for (i = 0; i < arrayExample.length; i++) {
            if (arrayExample[i] % 2 != 0) {
                System.out.print(arrayExample[i] + " ");
            }
        }
        int[] sumOfOdds = new int [1];
        sumOfOdds = sumOdds(newArray);
        return sumOfOdds;
    }
    public static int[] sumOdds(int[]arrayExample1) {
        int i;
        int[] oddsTotal = new int[1];
        int total = 0;
        for (i = 0; i < arrayExample1.length; i++) {
            if (arrayExample1[i] % 2 != 0) {
                total = total + arrayExample1[i];
            }
        }
        oddsTotal[0] = total;
        return oddsTotal;
    }
    public static void main(String[] args) {
        int [] mainArray = new int [5];
        mainArray[0] = 17;
        mainArray[1] = 92;
        mainArray[2] = 21;
        mainArray[3] = 984;
        mainArray[4] = 75;
      printOdds(mainArray);
      int [] oddSum = new int[1];
        oddSum = sumOdds(mainArray);
        System.out.println(oddSum);
    }
}

And I'm getting this as output: 17 21 75 [I@51016012

I have absolutely no idea where that second part is coming from, so any help would be awesome. Thanks!

  • 1
    You are printing the reference to oddSum array in the last line of main function, the odd characters are shown due to the print statement. If you want to print the contents of the oddSum array use something like: System.out.println(Arrays.toString(oddSum)); – Osama A.Rehman Jul 22 '20 at 19:47
  • 2
    When you print an object that hasn't provided an override for `toString` you get a default string as the result. `[` indicates that the object being printed is an array, and the `I` immediately following it shows the type of the array, in this case `int`. The digits following the `@` represent the object's address in memory. TLDR; `[I@51016012` is the result of printing an array. – JonK Jul 22 '20 at 19:49
  • 1
    @JonK The digits following the `@` do not represent the object's address in memory. It is the identity hashcode. The identity hashcode is stable, while objects (including arrays) can be moved around in memory by the garbage collector. – Mark Rotteveel Jul 23 '20 at 17:56

2 Answers2

1

well you are storing the result of the sum in an array and then you print the reference of type int[], that's why you get [I@51016012. so you need to print oddSum[0].

Jordy
  • 109
  • 5
0

It is not quite clear why you return int[] from the methods that just print and calculate the sum of the odd numbers.

So the code could be enhanced:

public static void printOdds(int[] arr) {
    for (int n : arr) {
        if (n % 2 == 1) {
            System.out.print(n + " ");
        }
    }
    System.out.println();
}

public static int sumOdds(int[] arr) {
    int total = 0;
    for (int n : arr) {
        if (n % 2 == 1) {
            total += n;
        }
    }
    return total;
}

Also, it may be worth to use Java 8+ stream to implement both tasks in one run:

import java.util.Arrays;

public class PrintAndSumOdds {

     public static void main(String [] args){
        int[] arr = {17, 92, 21, 984, 75};
        
        int sumOdds = Arrays.stream(arr) // get IntStream from the array
              .filter(n -> n % 2 == 1)   // filter out the odds
              .peek(n -> System.out.print(n + " "))  // print them in one line
              .sum(); // and count the sum (terminal operation)
        System.out.println("\nTotal odds: " + sumOdds);
     }
}

Output:

17 21 75 
Total odds: 113
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42