-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
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Amarnath K
  • 49
  • 1
  • 9
  • Consider the empty input `""` - what would happen? – Hulk Jun 24 '22 at 12:52
  • What would `index` be, if you provided an empty String for `originalWord`? – QBrute Jun 24 '22 at 12:53
  • @QBrute no, i saw that one, but ive initialised them – Amarnath K Jun 24 '22 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 – QBrute Jun 24 '22 at 13:02
  • @QBrute, with an empty string it returns the same error – Amarnath K Jun 24 '22 at 13:03
  • @QBrute ah yes i did initialise it but it just returns 0 with any string – Amarnath K Jun 24 '22 at 13:04
  • What exactly are you trying to achieve? It is not clear what you are attempting to do. – vPrototype Jun 24 '22 at 13:06
  • this snippet is just me trying to get a hold of why it returns zero as the output, i actually need to make words into pig latin, as per my homework for today – Amarnath K Jun 24 '22 at 13:12
  • I'm not sure what your question is - your code returns the index of the last character if it is a vovel, and `0` otherwise. To find the last vovel, start searching from the end, and stop as soon as you find a vovel. – Hulk Jun 24 '22 at 13:18
  • Please do not edit the answer to make it a different one. This invalidates all answer already given. If you have another question, ask that as a separate question. I rolled back your changes. – Jens Schauder Jun 24 '22 at 13:24

2 Answers2

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"));
}
  • 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 – Amarnath K Jun 24 '22 at 13:16
  • 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 Jun 24 '22 at 13:20
  • 1
    @vPrototype I just add `break;` I don't optimize him code. – Luận Nguyễn Minh Jun 24 '22 at 13:22
  • 2
    @AmarnathK It's not good to use redundant non-optimized code. – vPrototype Jun 24 '22 at 13:23
  • @vPrototype its nothing close to what i want to do, i just am too frustrated to optimize it – Amarnath K Jun 24 '22 at 13:25
  • @LuậnNguyễnMinh There is no reason for the `else` clause. – David Conrad Jun 24 '22 at 13:58
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.

vPrototype
  • 109
  • 1
  • 8
  • 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` – Amarnath K Jun 24 '22 at 13:08
  • 2
    @AmarnathK What are you trying to do? Could you please explain what your method is supposed to do? Edit it into your post. – vPrototype Jun 24 '22 at 13:08