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.