jeudi 17 octobre 2019

Java skipping over else if statement [duplicate]

This question already has an answer here:

When entering words that start with "th", the else if statement is supposed to execute, however for some reason it is being skipped over and the else statement is being executed instead. Please help me I see nothing wrong.

Ignore the generally sloppy code I'm new to this.

import java.util.*;

public class PigLatin {

public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Please enter a word ==> ");

        String englishWord = readWord(console);
        String pigLatinWord = convertWord(englishWord);
        printResult(englishWord, pigLatinWord);

    }

    public static String readWord(Scanner console) {
        String englishWord = console.next();
        return englishWord;

    }

    public static String convertWord(String englishWord) {
        boolean vowel = isVowel((englishWord.toLowerCase()).charAt(0));

        if (vowel == true) {
            String firstPartV = englishWord + "-way";
            return firstPartV;
        } else if ((englishWord.toLowerCase()).substring(0, 2) == "th") {
            String firstPartTh = englishWord.substring(2) + "-" + englishWord.substring(0, 2) + "ay";
            return firstPartTh;
        } else {
            String firstPartC = englishWord.substring(1) + "-" + englishWord.charAt(0) + "ay";
            return firstPartC;

        }

    }

    public static boolean isVowel(char c) {

        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            return true;
        } else {
            return false;
        }
    }

    public static void printResult(String englishWord, String pigLatinWord) {

        System.out.print(("\"" + englishWord + "\"\n") + "  in Pig Latin is\n" + ("\"" + pigLatinWord + "\"\n"));
    }

}

Aucun commentaire:

Enregistrer un commentaire