Can I rewrite this code without using any loops? and just if/then/else? package
ID: 3905123 • Letter: C
Question
Can I rewrite this code without using any loops? and just if/then/else?
package scramble;
import java.util.Scanner;
import java.lang.Math;
public class Scramble{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 5-letter word: ");
String word = input.next();
int wordLength = word.length();
if (wordLength == 5) {
word = word.toUpperCase(); //to uppercase
String scram_word = ""; //scrambled word
//variables
int i;
char c;
int j; //to iterate
//we need to generate random number 4 times (5 to 2)
for(j=5; j>=2; j--)
{
i = (int)(Math.random()*j); //generate random number between 0 and j
c = word.charAt(i); //get char to remove
scram_word = scram_word + c; //add char to scram_word
String before = word.substring(0,i); //string before c
String after = word.substring(i+1); //string after c
word = before + after; //word without c
}
scram_word = scram_word + word; //add last char
System.out.println("Scrambled word: " + scram_word); //print scram_word
String fake_word = ""; //fake word
String vowels = ""; //vowels
String consonants = ""; //consonants
//get all consonants and vowels
for(j=0; j<scram_word.length(); j++)
{
if(scram_word.charAt(j)=='A' || scram_word.charAt(j)=='E' || scram_word.charAt(j)=='I' || scram_word.charAt(j)=='O' || scram_word.charAt(j)=='U')
vowels = vowels + scram_word.charAt(j);
else
consonants = consonants + scram_word.charAt(j);
}
//add cons and vowels to fake_word alternatively
for(j=0; j<vowels.length()&&j<consonants.length(); j++)
fake_word = fake_word + consonants.charAt(j) + vowels.charAt(j);
//if any vowles remained add them to fake_word
if(vowels.length()>j)
fake_word = fake_word + vowels.substring(j);
//if any consonants remained add them to fake_word
if(consonants.length()>j)
fake_word = fake_word + consonants.substring(j);
//print fake_word
System.out.println("Fake word: " + fake_word);
}
else{
System.out.println("Word must be 5 letters");
System.exit(1);
}
}
}
Thanks
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
import java.lang.Math;
public class Scramble{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 5-letter word: ");
String word = input.next();
int wordLength = word.length();
if (wordLength == 5) {
word = word.toUpperCase(); //to uppercase
String scram_word = ""; //scrambled word
//variables
int i;
char c;
int j; //to iterate
//we need to generate random number 4 times (5 to 2)
int rand1, rand2, rand3, rand4;
String after, before;
Random rand = new Random();
rand1 = rand.nextInt(5);
rand2 = rand.nextInt(4);
rand3 = rand.nextInt(3);
rand4 = rand.nextInt(2);
c = word.charAt(rand1); //get char to remove
scram_word = scram_word + c; //add char to scram_word
before = word.substring(0,rand1); //string before c
after = word.substring(rand1+1); //string after c
word = before + after; //word without c
c = word.charAt(rand2); //get char to remove
scram_word = scram_word + c; //add char to scram_word
before = word.substring(0,rand2); //string before c
after = word.substring(rand2+1); //string after c
word = before + after; //word without c
c = word.charAt(rand3); //get char to remove
scram_word = scram_word + c; //add char to scram_word
before = word.substring(0,rand3); //string before c
after = word.substring(rand3+1); //string after c
word = before + after; //word without c
c = word.charAt(rand4); //get char to remove
scram_word = scram_word + c; //add char to scram_word
before = word.substring(0,rand4); //string before c
after = word.substring(rand4+1); //string after c
word = before + after; //word without c
scram_word = scram_word + word; //add last char
System.out.println("Scrambled word: " + scram_word); //print scram_word
String fake_word = ""; //fake word
String vowels = scram_word.replaceAll("[abcdfghjklmnpqrstvxyzABCDFGHJKLMNPRSTVWXYZ]", "");
String consonants = scram_word.replaceAll("[aeiouAEIOU]", "");
/* THIS LOOP CANNOT BE REMOVED SINCE THE ORDER OF CONSONANTS AND VOWELS MATTER */
//add cons and vowels to fake_word alternatively
for(j=0; j<vowels.length()&&j<consonants.length(); j++)
fake_word = fake_word + consonants.charAt(j) + vowels.charAt(j);
//if any vowels remained add them to fake_word
if(vowels.length()>j)
fake_word = fake_word + vowels.substring(j);
//if any consonants remained add them to fake_word
if(consonants.length()>j)
fake_word = fake_word + consonants.substring(j);
//print fake_word
System.out.println("Fake word: " + fake_word);
}
else{
System.out.println("Word must be 5 letters");
System.exit(1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.