-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 ")
    }
}
9
  • 2
    "create variable size of 2D-array" & "how to get each 2D-array" - There are no arrays in your code. Commented Jan 23, 2018 at 15:31
  • 1
    You do not have any 2 dimensional arrays? Commented Jan 23, 2018 at 15:31
  • 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? Commented Jan 23, 2018 at 15:34
  • 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<ArrayList<?>> Commented Jan 23, 2018 at 15:39
  • 1
    Something similar to this requirement is How to create a Multidimensional ArrayList in Java? which contains examples. Commented Jan 23, 2018 at 15:42

2 Answers 2

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);
    
        }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time to solve this question first! I will try best to understand every code you wrote here!
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()));

3 Comments

Thanks for your help to write down how to make 2D-arraylist!
@Let'sYo answer updated.
Thanks for your help again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.