1

I'm pretty new to java so I apologize if this is a dumb question but I've been starting from the basics and building up. I've gotten into arrays recently from a tutorial and I had them working in different instances but now they don't seem to output the correct values. Here's my code:

import java.util.Random;

class mainstart {
public static void main (String[] args) {
    Random rand = new Random();
    int freq[]=new int[7];

    for(int roll = 0; roll<1000; roll++){
        ++freq[1+rand.nextInt(6)];
    }

    System.out.println("Face\tFrequency");

    for(int face=1;face<freq.length;face++){
        System.out.println(face + "\t" + freq);
    }
}
}

This program is supposed to roll a dice 1000 times and list the frequency in a chart but when I run the code it gives me a sequence of numbers and letters like so:

Face    Frequency
1       [I@6d06d69c
2       [I@6d06d69c
3       [I@6d06d69c
4       [I@6d06d69c
5       [I@6d06d69c
6       [I@6d06d69c

I originally deviated a bit from the tutorial to help me remember it better vs straight up copying but even after that I couldn't get it working. I ended up copying line for line other than the class name and still couldn't get it to output the values even though I saw it working perfectly for someone else.

I'm not sure if I can link the video here but as for on my end was it something in the settings I changed? Or just something simple I've overlooked? I tried researching this but I didn't quite know where to look, I hope someone can help.

Graver
  • 25
  • 4
  • 6
    `System.out.println(face + "\t" + freq[face]);`... – Reimeus Feb 20 '16 at 15:30
  • Actually it does. You are requesting to output the memory address where `freq` is located, not the values stored in the array. – Auzias Feb 20 '16 at 15:31
  • pls don't forget: your array starts at index 0. while you start face at 1. – Jodn Feb 20 '16 at 15:34
  • Thanks guys for the quick response – Graver Feb 20 '16 at 15:35
  • 1
    @Auzias It isn't the memory location, it is just the `toString()` of the array, which includes the hashcode, which - contrary to popular believe - is unrelated to the memory address in most JVM implementations. – Mark Rotteveel Feb 20 '16 at 15:41

2 Answers2

0

You are printing an array to the output and the toString() method of the array is not so readebla that's the problem. If you want to print the elements of the array you should iterate over the array and print the actual element yourArray[index] or you can just use Arrays.toString(yourArray)

alex.parej
  • 322
  • 2
  • 9
0

modified code hope my code helps

     Random rand = new Random();
int freq[]=new int[7];

for(int roll = 0; roll<1000; roll++){
    ++freq[1+rand.nextInt(6)];
}

System.out.println("Face\tFrequency");

for(int face=1;face<freq.length;face++){
    System.out.println(face + "\t" + freq[face]);
}

happy coding

SmashCode
  • 713
  • 1
  • 8
  • 12