Answer to Question #218 in Java | JSP | JSF for Linda

Question #218
How to create a Java program that gives off 10 alphabets (a-z) randomly? There must be at least 2 vowels too.
1
Expert's answer
2010-06-02T05:32:57-0400
It will make a permutation of those ones which were taken from 2 combinations. First combination consists of randomly taken 2 vowels and the other one consists of 8 randomly taken consonants.
For example:
import java.util.ArrayList;
import java.util.HashSet;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {

ArrayList<Integer> vowels = new ArrayList<Integer>();

vowels .add(65);
vowels .add(69);
vowels .add(73);
vowels .add(79);
vowels .add(85);
//y and w are vowels
vowels .add(87);
vowels .add(89);
int counter = 0;
ArrayList<Integer> letters = new ArrayList<Integer>();
for(int i=0;i<10;i++){
int help = (int) (Math.random()*26 + 65);
letters.add(new Integer(help));
// System.out.print((char)(letters.get(i).intValue()));
if (vowels .contains(new Integer(help))) counter++;
}
// System.out.println(counter);
while (counter < 2){
int rand = (int) (Math.random()*10);
Integer a = letters.get(rand);
if (vowels.contains(a) == false){
int rand2 = (int) (Math.random()*6);
letters.set(rand, vowels.get(rand2));
counter++;
}
}
// System.out.println();
for(int i=0;i<10;i++){
System.out.print((char)(letters.get(i).intValue()));
}

}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS