-2

I am learning Java through self-reading. Right now I am doing a exercise. I am trying to create variable size of 2D-array then assign random number from 10 to 100 and put it into each array.

the problem I am encountering is don't know how to get each 2D-array out and put it into a string then show it through dialog after finish creating variable object.

here is my code.

import java.security.SecureRandom;
import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Random {
    public int randomNum;
    public String ID;

    public Random(String ID, int initialValue) {
        SecureRandom randomNumbers = new SecureRandom();
        this.ID = ID;
        this.randomNum = initialValue;
        int randomValue = randomNumbers.nextInt(99) + 1;
        randomNum = randomValue;
    }

    public int getRandomNum() {
        return randomNum;
    }

    public String getID() {
        return ID;
    }
}

class RandomText {
    public static void main(String[] args) {
        int ans = Integer.parseInt(JOptionPane.showInputDialog("How many random number you want to show?"));

        ArrayList < Random > randomNum = new ArrayList < Random > ();
        for (int i = 0; i < ans; i++) {
            randomNum.add(new Random("ID " + Integer.toString(i), 0));
        }
        String result;
        for (int i = 0; i < ans; i++) {
            result = result + ?????? +"\n";
        }
        JOptionPane.showMessageDialog(null, result ")
    }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
Let's Yo
  • 335
  • 1
  • 3
  • 11
  • 2
    "_create variable size of 2D-array_" & "_how to get each 2D-array_" - There are no arrays in your code. – takendarkk Jan 23 '18 at 15:31
  • 1
    You do not have any 2 dimensional arrays? – Kleo G Jan 23 '18 at 15:31
  • Maybe it's my bad... I am trying to solve the ans and find that someone using Arraylist and Loop to creat object. I am trying to do so. Is it my way to do that is not right to solve the ans? – Let's Yo Jan 23 '18 at 15:32
  • Actually I would like to do let say I want 5 number to show.. then... ID1 10 ID2 20 ID3 35 ID4 14 ID5 22 – Let's Yo Jan 23 '18 at 15:33
  • 1
    "Solve the ans" is an expression I am not familiar with. Are you asking how to print a List? Or how to loop over the contents of a List? – OH GOD SPIDERS Jan 23 '18 at 15:34
  • I am trying to create 2D-array list which contains ID and RandomNum through loop and get all the ID and RandomNum out from the array – Let's Yo Jan 23 '18 at 15:35
  • 1
    Well, you didn't create a 2D-ArrayList. You have a basic 1 dimensional arraylist of your custom "Random" class. A 2D-ArrayList would be one where every element of the outer ArrayList would be an ArrayList itself: `ArrayList>` – OH GOD SPIDERS Jan 23 '18 at 15:39
  • 1
    Something similar to this requirement is [How to create a Multidimensional ArrayList in Java?](https://stackoverflow.com/questions/4401850/how-to-create-a-multidimensional-arraylist-in-java) which contains examples. – J_D Jan 23 '18 at 15:42
  • Thanks for all you guys help first. Can anyone show me the link for "creating 2D-arraylist through loop"? – Let's Yo Jan 23 '18 at 15:42

2 Answers2

2

You're missing a couple of things to make this work.

  1. Add a toString() method inside your Random class. The toString() method in an object is used to change an object to a string representation of the local variables (In "Random" case, you want to return a string with the ID and the randomNum, see below code).

  2. String result; needs to be assigned an initial value in order to use '+='. Change it to String result = "";

  3. Now that we have a "toString()" method, you can append it to result with result = result + randomNum.get(i).toString();

    import java.security.SecureRandom;
    import javax.swing.JOptionPane;
    import java.util.ArrayList;
    
    public class Random {
        public int randomNum;
        public String ID;
    
        public Random(String ID,int initialValue){
            SecureRandom randomNumbers = new SecureRandom();
            this.ID = ID;
            this.randomNum = initialValue;
            int randomValue = randomNumbers.nextInt(99)+1;
            randomNum = randomValue;
        }
    
        public int getRandomNum(){
            return randomNum;
        }
    
        public String getID(){
            return ID;
        }
    
        public String toString(){
            return ID + ": " + randomNum;
        }
    }
    
    class RandomText{
        public static void main(String[] args) {
            int ans = Integer.parseInt(JOptionPane.showInputDialog
                    ("How many random number you want to show?"));
    
            ArrayList<Random> randomNum = new ArrayList<Random>();
            for (int i = 0; i < ans; i++) {
                randomNum.add(new Random("ID " + Integer.toString(i),0));
            }
            String result = "";
            for (int i = 0; i < ans; i++) {
                result = result + randomNum.get(i).toString() + "\n";
            }
            JOptionPane.showMessageDialog(null, result);
    
        }
    }
    
Jeremy Rako
  • 127
  • 7
  • Thanks for your time to solve this question first! I will try best to understand every code you wrote here! – Let's Yo Jan 23 '18 at 15:47
1

I don't see why you need a 2D array when you can just use the Random#getID method you created:

String result;
for (Random random : randomNum) {
    result += random.getID() " : " + random.getRandomNum() + "\n";
}

But here is a method that uses a Map to create a 2D array list:

    Map<String, List<Integer>> idNums = new HashMap<String, List<Integer>>();
    randomNum.stream().forEach(r -> {
        if (idNums.get(r.getID()) != null) {
            idNums.get(r.getID()).add(r.getRandomNum());
        } else {
            idNums.put(r.getID(), Arrays.asList(r.getRandomNum()));
        }
    });

    ArrayList<List<Integer>> ids = new ArrayList<List<Integer>>();
    idNums.entrySet().forEach(e -> ids.add(e.getValue()));
Cardinal System
  • 2,749
  • 3
  • 21
  • 42