I'm a college student in my first programming class and I'm having trouble with an assignment. The requirements to the assignment can be found here: http://cs.millersville.edu/~zoppetti/161/Lab8.html
Anyways, I'm using scanner to scan a string and tokenize it into words. However, I need the user's input to be returned without any spaces. Then I need to convert the into piglatin. I appreciate any advice anyone can give me.
Here's my code:
Expand|Select|Wrap|Line Numbers
- import java.util.Scanner;
- public class PigLatin {
- public static void main(String[] args) {
- printIntro();
- Scanner input = new Scanner(System.in);
- System.out.print("Please enter an English phrase or sentence ending with a period:");
- String sentence = input.nextLine();
- System.out.println("\"" +sentence+ "\"" + " ");
- Scanner word = new Scanner(sentence);
- System.out.println(" " + "in" + " " + "Pig" + " " + "Latin" + " "+ "is ");
- System.out.print("\"");
- while (word.hasNext()) {
- String pigLatin = word.next();
- System.out.print(convertPigLatinWord(pigLatin));
- }
- }
- public static void printIntro() {
- System.out.println("\t****"+ "Welcome to Program Translate to Pig Latin!" + "****");
- System.out.println("\t***"+ "This Program will translate an English Sentence" + "***");
- System.out.println("\t\t**" + "into Pig Latin." + "**");
- System.out.println();
- }
- public static String convertPigLatinWord(String englishWord) {
- int length = englishWord.length();
- if (englishWord.charAt(length - 1) == '.'
- || englishWord.charAt(length - 1) == '!'
- || englishWord.charAt(length - 1) == '?') {
- char ch = englishWord.charAt(0);
- String rest = englishWord.substring(1, length - 1);
- return (rest + "-" + ch + "ay" + englishWord.charAt(length - 1) + "\""+" ");
- } else if ((englishWord.charAt(0) == 't' || englishWord.charAt(0) == 'T')
- && englishWord.charAt(1) == 'h') {
- return (convertTh(englishWord)+" ");
- } else if (isVowel(englishWord.charAt(0))) {
- return (englishWord + "-way"+" ");
- } else {
- char ch = englishWord.charAt(0);
- String rest = englishWord.substring(1);
- return (rest + "-" + ch + "ay"+" ");
- }
- }
- public static String convertTh(String tWord) {
- String th = tWord.substring(0,2);
- String rest = tWord.substring(2);
- return (rest + "-" + th+ "ay");
- }
- public static String convertPigLatinPhrase(String englishPhrase){
- Scanner word= new Scanner(englishPhrase);
- String nextWord = word.next();
- int space=nextWord.indexOf(" ");
- String nospace= nextWord.substring(space+1,englishPhrase.length()) ;
- return(nospace+" ");
- }
- public static boolean isVowel(char c) {
- if (c == 'a' || c == 'A') {
- return (true);
- } else if (c == 'e' || c == 'E') {
- return (true);
- } else if (c == 'i' || c == 'I') {
- return (true);
- } else if (c == 'o' || c == 'O') {
- return (true);
- } else if (c == 'u' || c == 'U') {
- return (true);
- } else {
- return (false);
- }
- }
- }