0

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!

4
  • 1
    Well, this is a problem: j = 'E';. You want NameChar[j] = 'E'; at that point. Commented Sep 22, 2015 at 21:39
  • Right sorry just fixed that, but thank you! Commented Sep 22, 2015 at 21:40
  • You might want to use a Dictionary (I think Map in java?) to avoid that nasty if-else chain Commented Sep 22, 2015 at 21:49
  • 1
    Protip: try to break down your problem into more manageable chunks. If you can get your program to work on one word, then you can try to get it to work on a bunch of words. Otherwise you are presenting people with a lot of unnecessary overhead and "noise". See stackoverflow.com/help/mcve Commented Sep 22, 2015 at 22:16

1 Answer 1

2

The problem is the location of this loop:

         for (j = 0; j < NameChar.length; j++){
             System.out.print(NameChar[j]);
         }

You have it positioned inside another loop iterating the same variable over the same range. At the end of the first iteration of that containing loop, the inner loop prints out the whole word, and also increments j to the point where the containing loop will not perform any more iterations.

Instead, either lift that innermost loop out of the other loop over j, or else just eliminate the loop part and make it simply

        System.out.print(NameChar[j]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.