-1

Here's my code :

public class PigLatinizer {
    public static int pigLatinOut(String originalWord) {
        int index;
        for(int i=0;i<originalWord.length();i++){
            if(originalWord.charAt(i)== 'A'||originalWord.charAt(i)=='E' || originalWord.charAt(i) =='I'||originalWord.charAt(i)=='O' ||originalWord.charAt(i)=='U'){
                index = i;
            }
            else{index=0;}

        }
        return index;
    }
    public static void main(String[] args){
        System.out.println(pigLatinOut("TEST"));
    }
}

I dont know what exactly do here, but here's the output :

C:\Users\amarn\IdeaProjects\General Programs\src\PigLatinizer.java:13:16
java: variable index might not have been initialize
10
  • Consider the empty input "" - what would happen? Commented Jun 24, 2022 at 12:52
  • What would index be, if you provided an empty String for originalWord? Commented Jun 24, 2022 at 12:53
  • @QBrute no, i saw that one, but ive initialised them Commented Jun 24, 2022 at 13:00
  • @AmarnathK no you didn't initialize index, as the error message very clearly tells you. The compiler is not wrong. If you never enter the loop, then index is never initialized, and that's the problem here Commented Jun 24, 2022 at 13:02
  • @QBrute, with an empty string it returns the same error Commented Jun 24, 2022 at 13:03

2 Answers 2

1

You should break after finding any word in the if clause! Because your loop will always loop to the end and the last work does not contain any char you need then it will always return 0. You just need to add break;, that is all.

public class PigLatinizer {
public static int pigLatinOut(String originalWord) {
    int index=0;//edited, same result
    for(int i=0;i<originalWord.length();i++){
        if(originalWord.charAt(i)== 'A'||originalWord.charAt(i)=='E' || originalWord.charAt(i) =='I'||originalWord.charAt(i)=='O' ||originalWord.charAt(i)=='U'){
            index = i;
            break;
        }
        else{index=0;}

    }
    return index;
}
public static void main(String[] args){
    System.out.println(pigLatinOut("TEST"));
}
Sign up to request clarification or add additional context in comments.

6 Comments

yes i did try the break originally, it didnt work, and i was in a hurry, so somehow it didnt work, and i just did whatever this question was and then got confused, tried weird things, got even more confused and somehow made something that made it into the question
Your solution isn't good. It has a lot of useless blocks of code and is redundant. And why should it return 0 if all else fails? 0 is still an index, it does not indicate that there is not a vowel...
@vPrototype I just add break; I don't optimize him code.
@AmarnathK It's not good to use redundant non-optimized code.
@vPrototype its nothing close to what i want to do, i just am too frustrated to optimize it
|
0

The issue is that you have not initialized your variable index. Set it to some value, for example, 0, when you are declaring it (this is called initializing).

If you simply declare it without a value and try to retrieve it, the compiler will not know what to retrieve, because a default value hasn't been set. Your loop conditions aren't going to happen for sure, so when you are doing return index; the compiler is not sure what to do.

To answer your question, to return the index of the first character that is a vowel:

public static int pigLatinOut(String originalWord) {
        int index = 0;
        for (char c : originalWord.toLowerCase().toCharArray()) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'y') {
                return index;
            }
            index++;
        }
        return -1; // if there are no vowels

}

Just run a loop of the chars in the String and retrieve the index.

2 Comments

yes im sorry in the original question it was not initialised, it now is and it returns no error but with any String given to it, it returns the same 0
@AmarnathK What are you trying to do? Could you please explain what your method is supposed to do? Edit it into your post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.