So I am trying to change the vowels of this list of words. a would go to e, e would go to i, i would go to u, and u would go to a (Same for uppercase). For some reason my code doesn't seem to be changing the characters (that could be due to what I am printing out as well). Am I changing the characters in the character array and if so, is my output just incorrect? How should I fix this?
The words we had to sort are here:
Pineapple Reading Jester Dragon Table Column Football Sweater Clock Stripes Student Custodian Hat Computer Staircase Stairwell Food Electronic Phone Music Sling Orange Pasta Jacket Door Flag Glass Cake Hatch Intersect Soccer Hockey Writer Fallout Skyrim Pencil Cube Deer Moose Dog Prism Pyramid Wallet Tiger
This is my output:
CakeClockColumnComputerCubeCustodianDeerDogDoorDragonIlectronicFalloutFlagFoodFootballGlassHatHatchHockeyOntersectJacketJesterMooseMusicUrangePastaPencilPhonePineapplePrismPyramidReadingSkyrimSlingSoccerStaircaseStairwellStripesStudentSweaterTableTigerWalletWriter
My code (NOTE: the first two parts were just getting to words from a .txt file and putting the words in alphabetical order):
import java.io.*;
import java.util.Scanner;
public class WordsWordsWords {
public static void main(String[] args) throws IOException {
File temp = new File("words.txt"); // Searches for the file
Scanner file = new Scanner(temp); // Creates a Scanner
int count = 0; // Finds the number of words that are in the file
while (file.hasNextLine()) // Determines how many words are in the file
{
file.nextLine();
count++;
}
file.close(); // Closes the scanner
String[] wordarr = new String[count]; // Creates an array with each element in the file
Scanner listplacement = new Scanner(temp); // Creates a scanner
for (int i = 0; i < wordarr.length; i++) // Takes each element and assigns a name to each one.
{
wordarr[i] = listplacement.nextLine();
}
listplacement.close(); // Closes the scanner
System.out.println("Your list of names (unsorted):"); // Prints out array of unordered words
for (int i = 0; i < wordarr.length; i++) {
System.out.println(wordarr[i]);
}
for (int i = 0; i < wordarr.length - 1; i++) {
int low = i;
for (int j = i + 1; j < wordarr.length; j++) {
if (wordarr[j].compareTo(wordarr[low]) < 0) {
low = j;
}
}
String tempo = wordarr[low];
wordarr[low] = wordarr[i];
wordarr[i] = tempo;
}
System.out.println();
System.out.println("Alphabetized Names:");
for (int n = 0; n < wordarr.length; n++) {
System.out.println(wordarr[n]);
}
System.out.println();
System.out.println("Names with Switched Letters");
for (int i = 0; i < wordarr.length; i++) {
String NameString = wordarr[i]; // Changes letters of the code
char[] NameChar = NameString.toCharArray();
for (int j = 0; j < NameChar.length; j++) {
if (NameChar[j] == 'A') {
NameChar[j] = 'E';
} else if (NameChar[j] == 'E') {
NameChar[j] = 'I';
} else if (NameChar[j] == 'I') {
NameChar[j] = 'O';
} else if (NameChar[j] == 'O') {
NameChar[j] = 'U';
} else if (NameChar[j] == 'U') {
NameChar[j] = 'A';
} else if (NameChar[j] == 'a') {
NameChar[j] = 'e';
} else if (NameChar[j] == 'e') {
NameChar[j] = 'i';
} else if (NameChar[j] == 'i') {
NameChar[j] = 'o';
} else if (NameChar[j] == 'o') {
NameChar[j] = 'u';
} else if (NameChar[j] == 'u') {
NameChar[j] = 'a';
}
for (j = 0; j < NameChar.length; j++) {
System.out.print(NameChar[j]);
}
}
}
}
}
Thank You!
j = 'E';. You wantNameChar[j] = 'E';at that point.