Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
What am I doing wrong with this Java program?
I need to generate a series of random capital characters that are not vowels. I have created the following code but the compiler (Netbeans 6.9) always gives me the following error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
This error refers the first 'if' statement in the code. The rest of it appears to be OK. Here is the code:
public class RandomStringGenerator {
public static void main(String[] args) {
char symbol = 'A';
symbol = (char)(128.0*Math.random());
if (symbol > 'A' ?? symbol <= 'D' ) {
System.out.println("Here is the letter: " + symbol);
} else if (symbol>= 'F' && symbol <= 'H'){
System.out.println("Here is the letter: " + symbol);
} else if (symbol >= 'J' && symbol <= 'N') {
System.out.println("Here is the letter: " + symbol);
} else if (symbol >= 'P' && symbol <= 'D') {
System.out.println("Here is the letter: " + symbol);
} else if (symbol>= 'B' && symbol <= 'D') {
System.out.println("Here is the letter: " + symbol);
} else {
System.out.println("The program only generated a vowels. Please try again.");
}
}
}
Can anyone tell me what I am doing wrong?
3 Answers
- Anonymous1 decade agoFavorite Answer
Did you mean to put && instead of ?? in the first if statement?
Also, this is not a great way to do it. By taking 128*Math.random(), you are letting the character be any ASCII character. This means that with a high probability you will fail, since there are only 21 characters in this group that you are interested in. A quick fix would be to take:
symbol = (char) (26.0 * math.random() + 'A');
This means that symbol is a letter between 'A' and 'Z'. The only problem is that there are still vowels to deal with. To get rid of these, we want to somehow "get rid" of these options. A quick way would be to say "if it is an 'A', output a 'B'", etc. However, this isn't good, since then the probability of getting a B doubles. A not very elegant but functional solution would be to do this:
int offset = (int) (21 * math.random());
if(offset >= 3 && offset < 6) {
offset += 1;
} else if(offset >= 6 && offset < 11) {
offset += 2;
} else if(offset >= 11 && offset < 16) {
offset += 3;
} else {
offset += 4;
}
symbol = (char)('B' + offset);
What this does is it picks a random number from 0 to 20 (there are 21 non-vowels), and then looks for the "offset'th" character in the alphabet. The jumps are to skip a vowel. This ensures that all of the characters are picked with the same probability.
- 1 decade ago
What's ?? in the first if statement?
Btw, you might not like what I'm going to say:
Your program has too many if-then for something you want to accomplish.
You should use some kind of loop to do this, in the loop generate a random character, if it is 'A','E','I','O', or 'U' then ignore, decrement the loop counter (so you can always generate the string that is of specified sequence length), and continue. If it is not a vowel, append to a string, and at the end print it out.
- ruppenthalLv 45 years ago
the rationalization why the output is popping out like that's while you evaluate that's exactly what you asked it to do :P you're asking this technique to multiply calculate by way of itself in the previous printing it, and it did that. attempt making a for-loop and multiplying calculate by way of the for-loop counter.