0

I am very new in progamming and I want to make a simple task Is it possible to generate a custom char in Java from specific list?

For example, I want that the program would give me random char only from this chars A H F G D ?

Is it possible?

Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42
Niv
  • 129
  • 2
  • 13
  • 1
    You can have an array containing chars, and use `Math.random()` to get a random index on this array. – Lutzi May 08 '18 at 11:21
  • Yes, it is possible. Writing some code will get you there! – f1sh May 08 '18 at 11:22
  • So... you have a set of letters, and want to randomly select one? Take a look at [`java.util.Random`](https://docs.oracle.com/javase/7/docs/api/java/util/Random.html) (<-- link) and it's `nextInt` method. – S.L. Barth is on codidact.com May 08 '18 at 11:22

2 Answers2

2

There is a simple way to achieve getting pseudorandom element from given set of chars. You can use Random.nextInt() (from java.util package) for that purpose.

You could think of creating an array of chars, and then let that methods choose an element for you.

Here is an example with the use of Random class:

char[] array = new char[] {'A', 'H', 'F', 'G', 'D', };
Random rand = new Random();
char chosenOne = array[rand.nextInt(array.length)]; // there is a pseudorandomly chosen index for array in range of 0 inclusive to 5 exclusive

EDIT: According to your comment (you say there that you want to randomly choose elements from set of Strings of various length (that way they are no more chars (as chars are a single character - '1' or '0', not '10'), they are Strings then), the most effortless way to achieve that result I can think of, is to put a delimiter between these values in the String. Here is a possible implementation (I made some additional explanations in the comments to the code):

public static void main(String[] args) {
    String[] array = splitToSeparateElements("A,H,F,10,G,D,1,0,2000"); // store the result of calling splitToElements() with a
                                                            // given String as argument
    Random rand = new Random();
    for (int i = 0; i < 10; i++) { // this loop is just to print more results to console
        System.out.print(array[rand.nextInt(array.length)] + " "); // print pseudorandom element of array to the console
    }
}

public static String[] splitToSeparateElements(String inputString) {
    String[] splitted = inputString.split(","); // split String to separate Strings in the place of delimiter passed as argument to .split() method
    return splitted;
}

Sample output:

D 1 A A 2000 F 10 G 10 A 
Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
  • 1
    Well , Thank you – Niv May 08 '18 at 11:32
  • Actually, there is one solution to get a "random" value since `Math.random` will simply create an instance of `Random` if needed and call `nextDouble` which will required a bit of math to get the `int` needed. And please, use `rand.nextInt(array.length)` if you want to keep that solution useful. (I removed mine because it was a duplicated already...) – AxelH May 08 '18 at 11:34
  • @AxelH Thank you for giving this detailed explanation, I,wasn't aware of that. I'll edit my answer to delete that information. – Przemysław Moskal May 08 '18 at 11:35
  • This is explain in [`Math.random`](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#random%28%299) ;) – AxelH May 08 '18 at 11:36
  • more question: I succesfully make this ; but what I ask more is this on the string I involved numbers and it is work but when I want the number 10 it separate it to random 1 or 0 how can I do it '10'. – Niv May 08 '18 at 12:31
  • How would your program know if in that particular case you want 10 and not 1 and 0 separately? My answer doesn't deal with String input, it deals with chars, as you asked in your question. If you want to cut String into ints or other elements, you should provide a logic, why in that particular case you want 1 and 0 to be 10 and not 1 and 0, maybe implement your own parser covering your expectations and then fill array with results that you get after parsing the input String is completed. I think it's out of the scope of your question. – Przemysław Moskal May 08 '18 at 12:41
  • You right its out of the scope... I will training more to make that program.. – Niv May 10 '18 at 12:48
  • @NivBooskila I edited my answer with an attempt to solve a problem of getting elements of various length from given String. Please, let me know if it solves your problem. – Przemysław Moskal May 10 '18 at 19:15
0

Yes you can !!

static char getRandomChar(String s){//pass string "AHFGD" here
    Random random = new Random();
    int index = random.nextInt(s.length());
    return s.charAt(index);

}