0

I'm a new programmer trying to convert a decimal number to an octal number but when I try to print my array it keeps giving me a weird value. Any thoughts on why?

    int i = 0;
    int p = 1;
    int[] octalTotal = new int[16];
    int copyTotal = total;
    int value = 0;

    while (copyTotal >0) {
        value = copyTotal % 8;
        System.out.println(value);
        copyTotal = copyTotal / 8;
        octalTotal[p] = value;
        i++;
        System.out.println(octalTotal);
    }

Returns: 1 [I@7eda2dbb 2 [I@7eda2dbb 5 [I@7eda2dbb

  • Try Arrays.toString(octalTotal) in the System.out call. Edit: Yup, this fixes it. – Jason Feb 01 '20 at 00:34
  • 2
    Because that is how arrays print, there are **much** easier ways to convert from decimal to octal. `System.out.printf("%o%n", decimalValue);` – Elliott Frisch Feb 01 '20 at 00:35
  • 1
    You only ever update `octalTotal[1]` since `p` never changes. There are multiple problems, here. – David Conrad Feb 01 '20 at 00:36
  • Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – itwasntme Feb 01 '20 at 01:50

0 Answers0