I'm currently working on a program which is pig Latin. It allows a user enter a sentence. The program would take the user input and change every word in to pig latin which it takes the first character of a word and add it to end of word with "ay". For instance, bog would be "ogday" This is what I have been done so far

import java.util.Scanner;

public class PigLatinTest
{
	public static void main(String[] args)
	{
		Scanner kybd = new Scanner(System.in);
		System.out.println("Please enter a sentence?");
		String enSentence = kybd.nextLine();
		
		//StringBuilder buffer = new StringBuilder(enSentence);
		
	}
		
	public static String convertToLatin(String s)
	{
		StringBuilder buffer = new StringBuilder();
		
		String [] sentence = s.split("");
		
		String k ="ay";
		
		for (String i:sentence)
		{
			buffer.insert(buffer.lastIndexOf(i), buffer.charAt(0));
			buffer.insert(buffer.lastIndexOf(i), k);
			buffer.delete(0,2);
		}		
		
	}
}

I wonder whether I did my convertToLatin correct or not? My other question is after the for loop loop each word, will the data in sentence array change? Or do I need to store the change into other array in order to access the change data?
Thank you so much.

I wonder whether I did my convertToLatin correct or not?

Well, did you test it? What was the result?

No, it won't work. Can you provide some hints or similar examples that would help me to create the method to convert English into pig latin? This is what I have so far. For your information, I test the code already. it won't work. I have been trying for several hours.

import java.util.Scanner;

public class PigLatinTest
{
	public static void main(String[] args)
	{
		Scanner kybd = new Scanner(System.in);
		System.out.println("Please enter a sentence?");
		String enSentence = kybd.nextLine();
		
		String r = convertToLatin(enSentence );
		System.out.println(r);
	}
		
	public static String convertToLatin(String s)
	{
		
		String [] sentence = s.split("");
		StringBuilder buffer = new StringBuilder(sentence.toString() );

		
		String k ="ay";
		int i =0;
		
		for (i:buffer.charAt(i))
		{
			if(1>=i.length())
			
				return i;
			
			else
			{
				buffer.insert(buffer.lastIndexOf(i), buffer.charAt(0));
				buffer.insert(buffer.lastIndexOf(i), k);
				buffer.delete(0,2);
			}
			
			
			
		}	
		return buffer.toString();
		
	}
}

I'm not sure how to edit it. But I think I need to rework my logic a bit. Can you provide some clues or hints? My thought so far is I need to use the split method in string class(that is what the specification say) to slip every word in a sentence and store into a array. Then use the stringbuilder to change the characters orders. Then return the string. In my main method I can just call the method and convert it and print it.

Okay. I'm running to compiler error say that it cannot find symbol, but I already define and initialize.

import java.util.Scanner;
public class TestProgram
{

	public static void main(String[] args)
	{
    //Using the scanner input
	 
    Scanner input = new Scanner(System.in);
    System.out.println("\nType a sentence in English: ");
    String english = input.nextLine();
	
	String [] sentence = english.split(" ");
		
		String k;
		
		for (int b=0; b < sentence.length; b++)
		
			 k = new StringBuilder().append(sentence[b]).append("").toString();
			
				for (int p=0; p< k.length(); p++)
				
					k.insert(k.lastIndexOf(p), k.charAt(0));
					k.insert(k.lastIndexOf(p), k);
					k.delete(0,2);

				
		
		
		
	
    //String Str = pigLatin(english);
    //System.out.println(Str);
	}
}

The compiler mark at k.insert() and say cannot find symbol k. But I already define k and initialize above the for loop. Why it is marking it wrong?

k is defined as a String. There is no insert method in the String class.

Thanks now I have everything working now.

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.