0

I swear that I put them in right. They should be generating/using a 25 numbers from -100 to 100, and the programs I use compile perfectly fine, but output the following:

 ----jGRASP exec: java IntegerArrayRunner
Numbers Array: [I@3343c8b3
Numbers Array Element Count: 25
Numbers Array Sum: 0
Numbers Array Average: 0
Evens Array: [I@272d7a10
Evens Array Element Count: 25
Evens Array Sum: 0
Evens Array Average: 0
Odds Array: [I@1aa8c488
Odds Array Element Count: 25
Odds Array Sum: 0
Odds Array Average: 0
Negatives Array: [I@3dfeca64
Negatives Array Element Count: 25
Negatives Array Sum: 0
Negatives Array Average: 0

 ----jGRASP: operation complete.

Seriously, what's up with this? Why does it print out those odd combinations of symbols and characters? What do they even mean? Here are the programs used.

public class IntegerArrayMain
{      
   private int[] numbers = new int [25];
   private int[] evens = new int [25];
   private int[] odds = new int [25];
   private int[] negatives = new int [25];
   private int sum;
   private int numbersSum;
   private int evenSum;
   private int oddSum;
   private int negaSum;
   private int avg;
   private int numbersAvg;
   private int evenAvg;
   private int oddAvg;
   private int negaAvg;

   DiceClass rng = new DiceClass(200);

   //============================NUMBERS====================================

   /**
   *Generates the 25 numbers.
   */
   public int[] setNumbers()
   {
      for(int i = 0; i < numbers.length; i++)
      {
         numbers[0] = (rng.getRoll() - 100);
         sum += numbers[i];
      }
      return numbers;
   }
   /**
   *Returns the numbers.
   */
   public int[] getNumbers()
   {
      return numbers;
   }
   /**
   *Finds and returns the sum of the numbers.
   */
   public int setNumsSum()
   {
      int numbersSum = 0;
      for( int i = 0; i < numbers.length; i++) {
         sum += numbers[i];
      }
      return numbersSum;
   }
   /**
   *Returns the numbers' sum.
   */
   public int getNumsSum()
   {
      return numbersSum;
   } 
   /**
   *Finds and returns the average of the numbers.
   */
   public double setNumsAvg()
   {
       double numbersAvg = sum/numbers.length;
       return numbersAvg;
   }
   /**
   *Returns the numbers' average.
   */
   public int getNumsAvg()
   {
      return numbersAvg;
   }  
   //============================EVENS======================================

   /**
   *Locates the even numbers.
   */
   public void findEvens()
   {
      int n = 0;
      for(int i : numbers){
         if((i % 2) == 0){
         evens[n] = i;
         }
         n++;
      }
   }
   /**
   *Returns the even numbers.
   */
   public int[] getEvens()
   {
      return evens;
   }
   /**
   *Finds and returns the sum of the evens.
   */
   public int setEvensSum()
   {
      int evenSum = 0;
      for( int i = 0; i < evens.length; i++) {
         sum += evens[i];
      }
      return evenSum;
   }
   /**
   *Returns the even numbers' sum.
   */
   public int getEvensSum()
   {
      return evenSum;
   }  
   /**
   *Finds and returns the average of the evens.
   */
   public double setEvensAvg()
   {
       double evenAvg = sum/evens.length;
       return evenAvg;
   }
   /**
   *Returns the even numbers' average.
   */
   public int getEvensAvg()
   {
      return evenAvg;
   }  

   //============================ODDS=======================================

   /**
   *Locates the odd numbers.
   */
   public void findOdds()
   {
      int n = 0;
      for(int i : numbers){
         if((i % 1) == 0){
         odds[n] = i;
         }
         n++;
      }
   }
   /**
   *Returns the odd numbers.
   */
   public int[] getOdds()
   {
      return odds;
   }
   /**
   *Finds and returns the sum of the odds.
   */
   public int setOddsSum()
   {
      int oddSum = 0;
      for( int i = 0; i < odds.length; i++) {
         sum += odds[i];
      }
      return oddSum;
   }
   /**
   *Returns the odd numbers' sum.
   */
   public int getOddsSum()
   {
      return oddSum;
   }  
   /**
   *Finds and returns the average of the odds.
   */
   public double setOddsAvg()
   {
       double oddAvg = sum/odds.length;
       return oddAvg;
   } 
   /**
   *Returns the odd numbers' average.
   */
   public int getOddsAvg()
   {
      return oddAvg;
   }  

   //============================NEGATIVES==================================

   /**
   *Locates the negative numbers.
   */
   public void findNegatives()
   {
      int n = 0;
      for(int i : numbers){
         if((i % 2) == 0){
         negatives[n] = i;
         }
         n++;
      }
   }
   /**
   *Returns the negative numbers.
   */
   public int[] getNegatives()
   {
      return negatives;
   }
   /**
   *Finds and returns the sum of the negatives.
   */
   public int setNegativesSum()
   {
      int negaSum = 0;
      for( int i = 0; i < negatives.length; i++) {
         sum += negatives[i];
      }
      return negaSum;
   }
   /**
   *Returns the negatives numbers' sum.
   */
   public int getNegativesSum()
   {
      return negaSum;
   }  
   /**
   *Finds and returns the average of the negatives.
   */
   public double setNegativesAvg()
   {
       double negaAvg = sum/negatives.length;
       return negaAvg;
   }
   /**
   *Returns the negative numbers' average.
   */
   public int getNegativesAvg()
   {
      return negaAvg;
   }  
}

And the other one:

import java.util.Scanner;

public class IntegerArrayRunner
{
   public static void main(String[] args)
   {

      System.out.println("Numbers Array: " + getNumbers());
      System.out.println("Numbers Array Element Count: " + numbers.length());
      System.out.println("Numbers Array Sum: " + getNumsSum());
      System.out.println("Numbers Array Average: " + getNumsAvg());

      System.out.println("Evens Array: " + getEvens());
      System.out.println("Evens Array Element Count: " + evens.length());
      System.out.println("Evens Array Sum: " + getEvensSum());
      System.out.println("Evens Array Average: " + getEvensAvg());

      System.out.println("Odds Array: " + getOdds());
      System.out.println("Odds Array Element Count: " + odds.length());
      System.out.println("Odds Array Sum: " + getOddsSum());
      System.out.println("Odds Array Average: " + getOddsAvg());

      System.out.println("Negatives Array: " + getNegatives());
      System.out.println("Negatives Array Element Count: " + negatives.length());   
      System.out.println("Negatives Array Sum: " + getNegativesSum());
      System.out.println("Negatives Array Average: " + getNegativesAvg());

   }
}

5 Answers5

0

The weird characters that you are referring to is the memory address of that array.

You need to either iterate through your array to print the content, or simply use Arrays.toString(array) to print out contents of the array.

Try this on a separate Java file for practice:

int[] a = {3,5,4,31,23};
System.out.println(a);  // Prints [I@15bdc50
System.out.println(Arrays.toString(a)); // Prints [3, 5, 4, 31, 23]
wns349
  • 1,266
  • 1
  • 10
  • 20
0

you are printing the Array for example in the first print line:

System.out.println("Numbers Array: " + getNumbers());

you are printing the array reference in result "Numbers Array: " + getNumbers() will call the ToString()on getNumbers() result that will return the Hashcode of the Instance, you need to instead do a getNumbers().length i guess?

Sith Nazgûl Cat
  • 204
  • 2
  • 10
0

You're printing the value of an array object instead of the contents of the array. If you want to print the contents of the array, consider either writing a function to return a formatted string of the contents or alter your print statement to something like this:

Since you use this multiple times, I suggest you write a function to print a formatted string.

You can write import java.util.Arrays at the top of your file and use the Arrays.toString(numbers) function if you want to, but if you define a function then you can always change how it looks whenever you want to by editing your function.

public String arrayToString(int[] array)
{
    String s = "";
    for (int i; i < array.length - 1; i++)
    {
        s += s + ", ";
    }
    //add last element of the array to the string without a comma
    s += "" + array[array.length - 1];

    //if the array is [1, 2, 3, 4]
    //this will return "1, 2, 3, 4"
    return s;
}

And then just use the function value to print it:

System.out.println("Numbers array: " + arrayToString(numbers));

Mdev
  • 2,440
  • 2
  • 17
  • 25
0

Those "odd combinations of symbols and characters" are the hashcodes of the objects which are obtained by calling the hashcode() method on an object. When you print an object, Java automatically calls the toString() method on that object. Now, in some object (e.g. int) toString() has been overridden to print the actual value (check the course code for Integer). However, in the case of an array, the toString() method has not been overridden; so it falls back to what the Object class defines and it simply prints the hashcode of an object (check the source code of Object's toString()).

s16h
  • 4,647
  • 1
  • 21
  • 33
0

Your getNumbers(), getEvens(), getOdds(), getNegatives() are supposed to print the array contents? If so, you should read this: Java overloading toString() method when array of class is given

This question appears to be a duplicate.

Community
  • 1
  • 1