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

Recommended Answers

All 7 Replies

Member Avatar for coil

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 coil

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 coil

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.