Help: Pig Latin java program

I need help with reversing my command line argument.

Page 1 of 1

3 Replies - 5041 Views - Last Post: 02 December 2010 - 08:52 AM Rate Topic: -----

#1 Aya_Sama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 10

Help: Pig Latin java program

Posted 01 December 2010 - 07:44 PM

Hi, I'm new here and java is definitely not being my friend right now.
Basically for my lab, I have to write a program that will take an English sentence provided on the command-line and transforms all its words into lowercase, then translates them into PigLatin. The translation to PigLatin of a word is done by reversing the characters in the word and adding sh at the end of the reversed word. For example:
Dreams are the touchstones of our character is translated to PigLatin:
smaerdsh erash ehtsh senotshcuotsh fosh ruosh retcarahcsh

My TA provided a skeleton of the program that has two static methods:
Main: the main method that implements the logic to solve the problem.
reverseString: a private static void method that takes a String and returns the reverse
of that string.

This is what I have so far.
import java.util.Scanner;

public class PigLatin {
	
	/*
	 * Process the command-line arguments to translate
	 * them to Pig Latin
	 */
	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Please enter any sequence of characters:");
		for (int i = args.length-1; i >=0; i--){
			for (int j = args[i].length()-1; j>=0; j--){
				System.out.print(args[i].charAt(j));
			}
		System.out.print(" ");
		}
		System.out.println();
	}
		
	
	/*
	 * Returns the reverse of the String 
	 * passed through the parameter word.
	 */
	private static String reverse(String args) {
		return args;
		
			
			
	}

}



what I need help with is reversing the argument and finding a way to add "sh" at the end of each word in the array so i can eventually set it to .toLowerCase and display the word in pig latin. I hope someone will be able to help me with this :(. Also, is it possible to, instead of creating multiple new topics for my future questions, to just put all of my future question in this topic?

Is This A Good Question/Topic? 0
  • +

Replies To: Help: Pig Latin java program

#2 cdog5000  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 79
  • Joined: 31-October 09

Re: Help: Pig Latin java program

Posted 01 December 2010 - 08:11 PM

There are many ways of reversing strings, Just use google. to add sh to the end just append it in the StringBUffer or + "sh", Either way works.
public String reverse(String source) {
    int i, len = source.length();
    StringBuffer dest = new StringBuffer(len);

    for (i = (len - 1); i >= 0; i--)
      dest.append(source.charAt(i));
    return dest.toString() + sh;
  }


In terms of your future questions, I would say as long as they pertain to this main question it should be fine, If it is a new problem though then you may consider making a new topic.
Was This Post Helpful? -2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Help: Pig Latin java program

Posted 01 December 2010 - 09:29 PM

View Postcdog5000, on 01 December 2010 - 09:11 PM, said:

There are many ways of reversing strings, Just use google. to add sh to the end just append it in the StringBUffer or + "sh", Either way works.
public String reverse(String source) {
    int i, len = source.length();
    StringBuffer dest = new StringBuffer(len);

    for (i = (len - 1); i >= 0; i--)
      dest.append(source.charAt(i));
    return dest.toString() + sh;
  }

StringBuffer class has a reverse() method
You are complicating your life for nothing :)
Was This Post Helpful? 1
  • +
  • -

#4 Aya_Sama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 10

Re: Help: Pig Latin java program

Posted 02 December 2010 - 08:52 AM

View Postpbl, on 01 December 2010 - 08:29 PM, said:

View Postcdog5000, on 01 December 2010 - 09:11 PM, said:

There are many ways of reversing strings, Just use google. to add sh to the end just append it in the StringBUffer or + "sh", Either way works.
public String reverse(String source) {
    int i, len = source.length();
    StringBuffer dest = new StringBuffer(len);

    for (i = (len - 1); i >= 0; i--)
      dest.append(source.charAt(i));
    return dest.toString() + sh;
  }

StringBuffer class has a reverse() method
You are complicating your life for nothing :)

Thank you, I have found the StringBuffer class :D
import java.util.Scanner;

public class PigLatin {
	
	/*
	 * Process the command-line arguments to translate
	 * them to Pig Latin
	 */
	public static void main(String[] args) {
		
		args[0]=null;
		
		Scanner scan = new Scanner (System.in);
		
		System.out.println("Enter text to reverse: ");
		 args = scan.nextLine();
		args.toLowerCase();

		String reverse (String args) = new StringBuffer(args).
		reverse().append("sh").toString();

		}			
	
	/*
	 * Returns the reverse of the String 
	 * passed through the parameter word.
	 */
	private static String reverse(String args) {
		return (new StringBuffer(args)).reverse().append("sh").toString();

	}
}


im still having trouble with everthing under the public static void :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1