Reputation: 512
So i have this code that is supposed to generate random words of length the user decides with these conditions:
[1] Every other letter is a random vowel (letter + vowel + letter + vowel...etc)
[2] Two vowels cannot meet together (ie: bout, caravaa)
I got over the first part condition, and the second condition needs some edit.
i used an if statement to check if letter1
or letter2
happen to be vowels, and if true generate generate different letters. Problem is those newly generated letters have a chance of being vowels. How do i keep repeating the if statement until it's false? (Letter1 and Letter2 are not vowels)
import java.util.Random;
public class NickName_1
{
public static void main(String[] args)
{
java.util.Scanner scanner = new java.util.Scanner(System.in);
int letterNum = 6;
Random rn = new Random();
char letter1 = (char) (rn.nextInt(26) + 'a');
char letter2 = (char) (rn.nextInt(26) + 'a');
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
char vowel;
while (true)
{
//Ask user for word's length and store it
System.out.println();
System.out.print("Please enter the length of the word in letters: ");
letterNum = scanner.nextInt();
vowel = vowels[(int)(Math.random() * 5)];
for (int i = 0; i < (letterNum / 3); i++)
{
letter1 = (char) (rn.nextInt(26) + 'a');
letter2 = (char) (rn.nextInt(26) + 'a');
vowel = vowels[(int)(Math.random() * 5)];
// if letter1 or letter2 equal a vowel then regenerate letters
if (letter1 == 'a' || letter1 == 'u' || letter1 == 'i' || letter1 == 'e' || letter1 == 'o' ||
letter2 == 'a' || letter2 == 'u' || letter2 == 'i' || letter2 == 'e' || letter2 == 'o')
{
letter1 = (char) (rn.nextInt(26) + 'a');
letter2 = (char) (rn.nextInt(26) + 'a');
}
System.out.print(letter1 + "" + vowel + "" + letter2);
}
}
}
}
Upvotes: 0
Views: 1292
Reputation: 5445
You have mentioned about two criteria. But the code does not fulfill them even replacing if
with while
.
I have modified your code so that it could fulfill your criteria. I want to point out the major things below:
You are trying to print 3 letters at a time. That means input should be multiple of 3, otherwise the program would fail. I would suggest you to print a letter at a time.
Your need is letter + vowel + letter + vowel...
, but you are printing letter + vowel + letter + letter + vowel + letter...
. Rather I recommend keep a boolean
variable and change it in every turn, check whether it is the turn of vowel or consonant, then do the corresponding work in their own code blocks.
It is a bad practice to end up a program with exception. Thats why I have added a try-catch
and wrapped the input in it.
To make your code more convenient, Carcigenicate suggested a good way in this comment -- "Make a function or set and check for membership". I would suggest you to make another array for consonants as you made for vowels and pick a random one.
Here is the modified code in ideone. Below is your version of the code:
import java.util.Random;
/* Name of the class has to be "Main" only if the class is public. */
class NickName_1
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
java.util.Scanner scanner = new java.util.Scanner(System.in);
int letterNum = 6;
Random rn = new Random();
char letter = (char) (rn.nextInt(26) + 'a');
boolean shouldCons = true;
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
while (true)
{
//Ask user for word's length and store it
System.out.println();
System.out.print("Please enter the length of the word in letters: ");
try{
letterNum = scanner.nextInt();
}catch(Exception e){
break;
}
for (int i = 0; i < letterNum; i++, shouldCons^=true)
{
if(shouldCons) {
letter = (char) (rn.nextInt(26) + 'a');
while (letter == 'a' || letter == 'u' || letter == 'i' || letter == 'e' || letter == 'o')
{
letter = (char) (rn.nextInt(26) + 'a');
}
}
else letter = vowels[(int)(Math.random() * 5)];
System.out.print(letter);
}
}
}
}
Upvotes: 1
Reputation: 6657
Just replace your if statement with a while statement.
while (letter1 == 'a' || letter1 == 'u' || letter1 == 'i' || letter1 == 'e' || letter1 == 'o' ||
letter2 == 'a' || letter2 == 'u' || letter2 == 'i' || letter2 == 'e' || letter2 == 'o')
{
letter1 = (char) (rn.nextInt(26) + 'a');
letter2 = (char) (rn.nextInt(26) + 'a');
}
Update:
Looking at your code, it would be much better to check each of the letters separately, otherwise letter1 could be getting consonants but because letter 2 is not it continues changing both of them.
while (letter1 == 'a' || letter1 == 'u' || letter1 == 'i' || letter1 == 'e' || letter1 == 'o')
{
letter1 = (char) (rn.nextInt(26) + 'a');
}
while (letter2 == 'a' || letter2 == 'u' || letter2 == 'i' || letter2 == 'e' || letter2 == 'o')
{
letter2 = (char) (rn.nextInt(26) + 'a');
}
Upvotes: 0