Reshad
Reshad

Reputation: 2652

Iterate random integers into an Array

Hello everyone I am trying to Iterate trough an Array and fill each Array element with a random value. the only problem is that i get weird outputs like

[I@4b142196

the example for this I found on Array Example

but when using this in my code it won't work. Here is what I got:

package h05GrootsteWaarde;
import javax.swing.*;
import java.util.*;

public class GetallenGenerator extends JPanel {
    public GetallenGenerator() {
    }
    int[] val = new int[21];
    Random generator = new Random();    
    public void setRandomValue() {
        for (int i = 0; i < val.length; i++) {
            val[i] = generator.nextInt(201) - 100;
        }    
    }   
    public int[] getRandomValue() {     
        return val;     
    }
}

and here is how I call the function

package h05GrootsteWaarde;
import javax.swing.*;
import java.awt.event.*;
public class Bediening extends JPanel implements ActionListener {
    GetallenGenerator generator;
    private JButton bereken;
    private JTextArea veld;

    public Bediening(GetallenGenerator generator) {     
        this.generator = generator;     
        bereken = new JButton("Bereken kleinste");
        bereken.addActionListener(this);
        add(bereken);       
        veld = new JTextArea(13, 40);
        veld.setEditable(false);
        veld.setWrapStyleWord(true);
        veld.setLineWrap(true);
        add(veld);      
        generator.setRandomValue();
    }   
    public void actionPerformed ( ActionEvent e ) {     
        String hoi = " " + generator.getRandomValue();
        veld.append(hoi);       
    }
}

Upvotes: 2

Views: 565

Answers (5)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

You get a strange output because array's toString method does not show the string containing individual members of the array; instead, it shows a strange string starting in [I@ for arrays of primitive type int.

You should prepare the string manually, or call Arrays.toString(myArray), like this:

int[] arr = generator.getRandomValue();
String arrStr = Arrays.toString(arr);
String hoi = arrStr.substring(1, arrStr.length()-1);

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500535

The method:

public int[] getRandomValue() {
    return val;
}

... doesn't return a random value from the array - it returns the array reference itself. You're seeing the [I@4b142196 string because that's the default string representation of an array. (The first part indicates the type; the second part is a hash code.)

If you want to get a random value from the array, you'll need to actually put some randomness into getRandomValue...

Upvotes: 2

Rohit Jain
Rohit Jain

Reputation: 213243

The method generator.getRandomValue(); returns an integer array. So, if you print it, it prints the String Representation of your array, which you see is something like a HashCode for the array reference. So when you try to append it with an empty string and store it in another string, you will not get the desired result.

You can probably use is: -

String hoi = " " + Arrays.toString(generator.getRandomValue());

But why are you appending and then storing your integer array to a String reference?

If you want some random value out, then probably, you can use an ArrayList and use Collections.shuffle on that list to shuffle it. If you want a single random value, then you can fetch the first value. But that won't be very efficient way to get a random value. Because it will not be random enough.

Upvotes: 2

Abubakkar
Abubakkar

Reputation: 15654

You need to override toString method if you want the generated values. Here in the code that you have wrote will give you the memory reference and not the generated values.

Upvotes: 0

Dan D.
Dan D.

Reputation: 32391

You are adding an int[] to your JTextArea. It displays the result of calling toString() on your int[] which is the memory reference.

You should probably modify your getRandomValue() method to something like this:

public String getRandomValue() {
  return Arrays.toString(val);
}

Upvotes: 0

Related Questions