Right now Iam writing a program that will convert english into piglatin. Ans I have pretty much finished the program, but Iam stuck and need help.

When I run the program, nothing happens after I type in the users input. What am I doing wrong.

Heres my code,

import java.util.Scanner; 
  
public class Example { 
  
    public static void main(String[] args) { 
     
    	String englishWord, pigLatin;	
    	
    	Scanner kbd = new Scanner(System.in);
    	System.out.println("Enter word that you want to translate: ");
    	String english = kbd.nextLine();
    
    	while(english.indexOf(' ') != -1)	
    	{
    	englishWord = english.substring(0,english.indexOf(' '));
    	english = english.substring(english.indexOf(' '));
    	
    	if(englishWord.charAt(0)=='a' || englishWord.charAt(0)=='e' || englishWord.charAt(0)=='i' || englishWord.charAt(0)=='o' || englishWord.charAt(0)=='u')
    	{
    	pigLatin = englishWord + "way";
    	System.out.println(pigLatin);
    	}
    	else
    	{
    	char firstChar = englishWord.charAt(0);
    	pigLatin = englishWord.substring(1,englishWord.indexOf(' '))+ firstChar +"ay";
    	System.out.println(pigLatin);
    	}
    	pigLatin = englishWord + " ";
    	}
    	}
    	}

Any help would be thankful.

Thanks

Dani AI

Generated

You are close. The freeze comes from your while condition and slicing. As @coil hinted, you never advance past the space: english = english.substring(english.indexOf(' ')); leaves the space at the front, so indexOf(' ') keeps returning 0 and the loop never ends. Use + 1 to skip it. In the else branch you call englishWord.indexOf(' '), but a single word has no spaces; that returns -1 and will throw. You also overwrite pigLatin after printing, and translate(...) is never called.

A simpler, safer pattern: read a full line, split on whitespace, translate each word, then join the results. Here is a compact drop-in example that uses a dedicated translate method:

import java.util.*;

public class Example {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter text: ");
    String line = sc.nextLine().trim();
    StringBuilder out = new StringBuilder();
    for (String w : line.split("\\s+")) {
      if (out.length() > 0) out.append(' ');
      out.append(translate(w));
    }
    System.out.println(out.toString());
  }

  static String translate(String w) {
    String s = w.toLowerCase();
    String vowels = "aeiou";
    int i = 0;
    while (i < s.length() && vowels.indexOf(s.charAt(i)) == -1) i++;
    if (i == 0) return s + "way";
    if (i == s.length()) return s + "ay";
    return s.substring(i) + s.substring(0, i) + "ay";
  }
}

Extras: add a break in ’s vowel scan so it prints once per word, not once per vowel. Later you can preserve capitalization and punctuation, but get the basic flow working first.

Recommended Answers

All 7 Replies

Member Avatar for Member #814414

First, please use [code] tags when posting. It makes your code a lot easier to read.

Secondly, put a System.out.println(english) call right before your while loop. This verifies that you actually are taking in input.

If you find out that 'english' is actually empty, try using kbd.next() instead of kbd.nextLine();

Well now when I do that, all that comes up is the users input. Say if I type in yellow, the output is yellow. Nothing changed.

Member Avatar for Member #814414

Try moving your System.out.println statement (where you print out the pig-latin version) outside of the while-loop and after it.

Also, please repost your code.

When I did that all that it did was print the users input twice.

And oh yea I forgot, I have to put in a created method called translate. And that is to get the Pig Latin translation of the input word. Could you help me on how to put that in?

Heres what I have so far,

import java.util.Scanner; 

public class Example { 

public static void main(String[] args) { 

String englishWord, pigLatin; 

Scanner kbd = new Scanner(System.in);
System.out.println("Enter word that you want to translate: ");
String english = kbd.nextLine();

System.out.println(english); 
while(english.indexOf(' ') != -1) 
{

englishWord = english.substring(0,english.indexOf(' '));
english = english.substring(english.indexOf(' '));

if(englishWord.charAt(0)=='a' || englishWord.charAt(0)=='e' || englishWord.charAt(0)=='i' || englishWord.charAt(0)=='o' || englishWord.charAt(0)=='u')
{

pigLatin = englishWord + "way";
System.out.println(pigLatin);

}

else
{

char firstChar = englishWord.charAt(0);
pigLatin = englishWord.substring(1,englishWord.indexOf(' '))+ firstChar +"ay";
System.out.println(pigLatin);

}

pigLatin = englishWord + " ";

}

System.out.println(english); 
}

}

Ok Iam about done with this english to piglatin program, but I dont know what Iam missing for it to be done and also for it to work. Can anyone tell me what Iam missing or doing wrong?

Heres my code,

import java.util.Scanner; 
  
public class Example { 
  
    public static void main(String[] args) { 
    
    	Scanner kbd = new Scanner(System.in);
    	System.out.print("Please enter an English word:"); 
    	String word = kbd.nextLine(); 
    	System.out.println("Your word" + " " + "in" + " " + "Pig" + " " + "Latin" + " "+ "is "); 
    	System.out.println(word); 
    
    	String englishWord;
    	
    	while(word.indexOf(' ') != -1) 
    	{
    	englishWord = word.substring(0,word.indexOf(' '));
    	word = word.substring(word.indexOf(' '));
    	}
    }
    	public static void translate (String englishWord)
    	{
    		String pigLatin;
			if (englishWord.charAt(0)=='a' || englishWord.charAt(0)=='e' || englishWord.charAt(0)=='i' || englishWord.charAt(0)=='o' || englishWord.charAt(0)=='u')
			{	
    			pigLatin = englishWord + "way";
    			System.out.println(pigLatin);
			}
			
			else 
			{
				char firstChar = englishWord.charAt(0);
				pigLatin = englishWord.substring(1,englishWord.indexOf(' '))+ firstChar +"ay";
				System.out.println(pigLatin);
			}

    	}
    	
    }
Member Avatar for Member #814414

Once again, please use code-tags when posting code.

Why is your while-loop terminating condition english.indexOf(' ')!=-1? First, what if I enter in only one word without spaces? And even if I do enter in multiple words separated by spaces, the condition never changes, so it's an infinite loop.

Once you get all of this working, you can put it into a method like so:

public String translate(String english) {
        String translated;
        //Translate
        return translated;
}
import java.util.Scanner; 

public class Example { 

public static void main(String[] args) { 

Scanner kbd = new Scanner(System.in);
System.out.print("Please enter an English word:"); 
String word = kbd.nextLine(); 
System.out.println("Your word" + " " + "in" + " " + "Pig" + " " + "Latin" + " "+ "is "); 


/*
String word="";

  while(word.indexOf(' ') != -1) 
  {
    word = word.substring(0,word.indexOf(' '));
    word = word.substring(word.indexOf(' '));
  }
    System.out.print(word);
}

*/
String pigLatin;

  if (word.charAt(0)=='a' || word.charAt(0)=='e' || word.charAt(0)=='i' || word.charAt(0)=='o' || word.charAt(0)=='u')
  { 
     pigLatin = word + "way";
     System.out.println(pigLatin);
  }

  else 
  {
   
     for (int i=0; i < word.length(); i ++){
	   if(word.charAt(i)=='a' || word.charAt(i)=='e' || word.charAt(i)=='i' ||
		   word.charAt(i)=='o' || word.charAt(i)=='u') {
			  String app = word.substring(0,i);
			  //System.out.println(app+"\n");
			  String temp = word.substring(i,word.length());
			  //System.out.println(temp);
			  System.out.println(temp+app+"ay");
		 }
			
    // char firstChar = word.charAt(0);
    // pigLatin = word.substring(1,word.indexOf(' '))+ firstChar +"ay";
    // System.out.println(pigLatin);
  }
  

}

}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.