5 Replies - 12509 Views - Last Post: 05 November 2012 - 09:26 AM Rate Topic: -----

#1 thoulihan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-November 12

Simple Pig Latin Assignment

Posted 03 November 2012 - 12:00 PM

I am a beginner at Java and have an assignment to design a Pig Latin translator. I had no trouble with it at all until i realized that my converter only does it for one word, I think what i need to do is break up the String into separate words. Another problem I'm having is that when a word begins with a capital letter ( e.g Hello = elloHay ) i don't know how i can make the 'H' become lowercase. I have been searching online for hours and it seems that everyone out there uses a different type of java to me, which is a bit confusing but any help/advice would be greatly appreciated as the deadline is very soon! Thanks. Here is what i have so far:

import java.util.Scanner;

public class pigLatin
{
    public static void main(String[] args)
    {
        Scanner stdIn=new Scanner(System.in);
        
        String word;
        String newWord;
        
        String repeatAgain = "y";
        
        while (repeatAgain.equals("Y") || repeatAgain.equals("y"))
        {
            System.out.print("\f");
            System.out.print("Enter a word: ");
            word=stdIn.nextLine();
        
            if (word.charAt(0)=='a' ||word.charAt(0)=='A' || word.charAt(0)=='e' || word.charAt(0)=='E' || word.charAt(0)=='i' || word.charAt(0)=='I' || word.charAt(0)=='o' || word.charAt(0)=='O' || word.charAt(0)=='u' || word.charAt(0)=='U')
                {
                        newWord= word + "way";
                    }
        
            else 
                {
                        newWord=word.substring(1) + word.charAt(0) + "ay";
                    }
                    
            System.out.println("That word translates to " + newWord + " in Pig Latin");
            
            System.out.println("Do you want to play again? (y/n)");
            repeatAgain = stdIn.nextLine();
        }
        
    
}
}




Thank you!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Pig Latin Assignment

#2 Bountyhunter1234   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 76
  • Joined: 12-May 09

Re: Simple Pig Latin Assignment

Posted 03 November 2012 - 10:14 PM

hey i am also a beginner but i think i can point you in the right direction. First if you didn't know of this website java dox this site has both of the answers you will need by looking at the String class and the Scanner class.

if you want the code try this

import java.util.Scanner;

public class pigLatin
{
    public static void main(String[] args)
    {
        Scanner stdIn=new Scanner(System.in);
        
        String word;
        String newWord;
        
        String repeatAgain = "y";
        
        while (repeatAgain.equalsIgnoreCase("y"))
        {
            System.out.print("\f");
            System.out.print("Enter a word: ");
            word=stdIn.nextLine();
            Scanner token = new Scanner(word);
            String words; 
            String pigLatinWord= "";
            while(token.hasNext()){
                words = token.next();
                if (words.charAt(0)=='a' ||words.charAt(0)=='A' || words.charAt(0)=='e' || words.charAt(0)=='E' || words.charAt(0)=='i' || words.charAt(0)=='I' || words.charAt(0)=='o' || words.charAt(0)=='O' || words.charAt(0)=='u' || words.charAt(0)=='U'){
                        newWord= words + "way";
                }else{
                        newWord=words.substring(1).toLowerCase() + words.charAt(0) + "ay";
                }
                pigLatinWord += newWord+ " ";
            }
            System.out.println("That word translates to " + pigLatinWord + " in Pig Latin");
            
            System.out.println("Do you want to play again? (y/n)");
            repeatAgain = stdIn.nextLine();
        }
        
    
}
}



you might wanna try a Boolean statement for your while statement instead of checking for Y mostly because if you type anything but y it will close the program. I hope this helped you
Was This Post Helpful? 1
  • +
  • -

#3 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Simple Pig Latin Assignment

Posted 04 November 2012 - 01:04 AM

newWord=words.substring(1).toLowerCase() + words.charAt(0) + "ay";



Although I haven't tested it, I'm pretty sure this won't work, since the sub-string is already lower case. thoulihan1, the Character API has a method for creating lower case letters. And as for separating a string into words, you can String[] words = myString.split(" ").

This post has been edited by blackcompe: 04 November 2012 - 01:04 AM

Was This Post Helpful? 1
  • +
  • -

#4 Bountyhunter1234   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 76
  • Joined: 12-May 09

Re: Simple Pig Latin Assignment

Posted 04 November 2012 - 12:12 PM

haha that's what i get for coding while im tired. So the toLowerCase definitely doesn't work sorry about that.
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Simple Pig Latin Assignment

Posted 04 November 2012 - 08:02 PM

this is an horror to call 20 times the same method
if (word.charAt(0)=='a' ||word.charAt(0)=='A' || word.charAt(0)=='e' || word.charAt(0)=='E' || word.charAt(0)=='i' || word.charAt(0)=='I' || word.charAt(0)=='o' || word.charAt(0)=='O' || word.charAt(0)=='u' || word.charAt(0)=='U')  
                 {  


you have shares in CPU companies like Intel or AMD or what ?

at least do
char letter = word.charAt(0);
if (letter=='a' ||letter=='A' || letter=='e' || letter=='E' || ....


Was This Post Helpful? 0
  • +
  • -

#6 thoulihan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-November 12

Re: Simple Pig Latin Assignment

Posted 05 November 2012 - 09:26 AM

pbl i didn't know you could do that, but thanks!

yeah the toLowerCase didn't work but the rest is great. Really appreciate everyone's help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1