0

I made a Java Script that took word input and turned it into a Pig Latin version of said word:

import java.util.Scanner;

class Main {
  static Scanner myObj = new Scanner(System.in);

  static boolean isVowel(char c) {
    return (c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U'
        || c == 'u');
  }

  static String pigLatin(String oldWord) {
    System.out.println("\r\n");
    System.out.println("What word should I translate?");
    oldWord = myObj.nextLine();
    System.out.println("");
    try {
      Thread.sleep(800);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print("\r\n");
    System.out.print("\r\n");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    int len = oldWord.length();
    int index = -1;
    for (int i = 0; i < len; i++) {
      if (isVowel(oldWord.charAt(i))) {
        index = i;
        break;
      }
    }

    if (index == -1)
      return "-1";

    return oldWord.substring(index) + oldWord.substring(0, index) + "ay";
  }

  public static void main(String[] args) {
    String newWord = pigLatin("graphic");
    if (newWord == "-1")
      System.out.print("No vowels found. Pig Latin not possible");
    else {
      System.out.print("Your word in Pig Latin is: \033[1m" + newWord + "\033[0m");
    }
  }
}

This takes whatever is typed, moves all characters before the first vowel to the end, and then adds "ay" to it. I am now trying to change this to make it translate a sentence instead, so I decided to try using a string array for this, but I probably did something stupid or missed something important because I ended up with this code:

import java.util.Scanner;

class Main {
  static Scanner myObj = new Scanner(System.in);

  static boolean isVowel(char c) {
    return (c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U'
        || c == 'u');
  }

  static String pigLatin(String sentance) {
    System.out.println("\r\n");
    System.out.println("What sentance should I translate?");
    String oldWord = myObj.nextLine();
    System.out.println("");
    try {
      Thread.sleep(800);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print("\r\n");
    System.out.print("\r\n");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    
    String[] words = oldWord.split(" ");
    for (String word : words) {
      int len = word.length();
      int index = -1;
      for (int i = 0; i < len; i++) {
        if (isVowel(word.charAt(i))) {
          index = i;
          break;
        }
      }
      if (index == -1)
        return "-1";
      return word.substring(index) + word.substring(0, index) + "ay";
    }
    return words.toString();
  }

  public static void main(String[] args) {
    String newWord = pigLatin("graphic");
    System.out.print("Your sentance in Pig Latin is: \033[1m" + newWord + "\033[0m");
  }
}

Which just made it only translate the first word and delete the rest. I'm still new to Java, and I have no idea how to fix this problem.

RETERON
  • 3
  • 1
  • 1
    You have to take a look to these lines: ```return "-1";```, ```return word.substring(index) + word.substring(0, index) + "ay";```. Return within a for-loop does not mean it will continue with the loop, but your function is finished immediately and with that value returned. – Enowneb Feb 09 '23 at 01:05
  • @Enowneb Thank you, as I didn't know this about loops, but is there a way to get around this? Or maybe a way to replace using returns inside of the loop? – RETERON Feb 09 '23 at 20:28

1 Answers1

0

The reason why the code will just return the first word and stop processing the remaining lies here:

String[] words = oldWord.split(" ");
for (String word : words) {
    int len = word.length();
    int index = -1;
    for (int i = 0; i < len; i++) {
        if (isVowel(word.charAt(i))) {
            index = i;
            break;
        }
    }
    if (index == -1)
        return "-1"; // Stop processing anymore
    return word.substring(index) + word.substring(0, index) + "ay"; // Stop processing anymore
}

The above is the for-loop that processes all the separate words from your sentence. However, a return statement will stop proceeding your pigLatin() function further and directly returns that value. So if you want to process all words from the sentence, you must not call return within the for-loop.

So what you can do, is to create another variable to hold your processed words. Like:

String[] words = oldWord.split(" ");
// Create another String array to hold your processed words
String[] pigLatinWords = new String[words.length];
int wordCounter = 0;
for (String word : words) {
    int len = word.length();
    int index = -1;
    for (int i = 0; i < len; i++) {
        if (isVowel(word.charAt(i))) {
            index = i;
            break;
        }
    }
    if (index == -1) {
        // Store the processed word in this new Array
        pigLatinWords[wordCounter++] = word;
        // Continue with the for-loop without processing the code below
        continue;
    }
    // Store the processed word in this new Array
    pigLatinWords[wordCounter++] = word.substring(index) + word.substring(0, index) + "ay";
}

And at the end you should call return like:

return String.join(" ", pigLatinWords);

Or you should refer to this to implement your own concatenation function if you are using Java 7 or below.

Changing the above code should produce a result matching with your expectation:

What sentance should I translate?
graphic graphic

...

Your sentance in Pig Latin is: aphicgray aphicgray

And note that you should not call toString() on String[], or you will end up having something like the hexadecimal hashcode of String[] printed:

Your sentance in Pig Latin is: [Ljava.lang.String;@7a79be86

Enowneb
  • 943
  • 1
  • 2
  • 11