Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Pig Latin Translator

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pig Latin Translator

    So one of the last programs I had to do was a Pig Latin Translator. I completed the project at home and it worked perfectly, but it will not work on the school's computers. I found out that the school computers still use 1.4.2 and I was using 1.5. How would I go about making my program work on the school computers.

    import java.util.Scanner;
     
     
    public class PigLatin
    {
     
       public static void main (String[] args)
       {
          String sentence, result, another;
     
          Scanner scan = new Scanner (System.in);
     
          do
          {
             System.out.println ();
             System.out.println ("Please enter a sentence : ");
             sentence = scan.nextLine();
     
             System.out.println ();
             result = PigLatinTranslator.translate (sentence);
             System.out.println ("That sentence in Pig Latin is: ");
             System.out.println (result);
     
          }
          while (another.equalsIgnoreCase("y"));
       }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Pig Latin Translator

    Hello BMN901, welcome to the Java Programming Forums

    I remember Pig Latin from when I was at school. Good times!

    The problem is with this piece of code:

    result = PigLatinTranslator.translate (sentence);
    You do not have the PigLatinTranslator.translate() method in your code.

    You can fix it like this but you will need to add the translator code.

    import java.util.Scanner;
     
    public class PigLatin
    {
        public static String translate(String sentance){
     
            //Translate code here
     
            return sentance;
     
        }
     
       public static void main (String[] args)
       {
          String sentence, result;
          Scanner scan = new Scanner (System.in);
     
          System.out.println ("Please enter a sentence : ");
     
          while(scan.hasNext()){
             sentence = scan.nextLine();
             result = PigLatin.translate(sentence);
             System.out.println ("That sentence in Pig Latin is: ");
             System.out.println (result);
             System.out.println ("Please enter a sentence : ");
          }
     
       }
    }
    I run Java 1.6 so I have no idea if this will work with 1.4.2.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pig Latin Translator

    I thought import java.util.Scanner; was introduced in 1.5. The school computers use 1.4. I'm still get error's. I guess what I'm trying to ask is, is there an alternate to using the Scanner so I can make it run on 1.4?

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Pig Latin Translator

    Quote Originally Posted by BMN901 View Post
    I thought import java.util.Scanner; was introduced in 1.5. The school computers use 1.4. I'm still get error's. I guess what I'm trying to ask is, is there an alternate to using the Scanner so I can make it run on 1.4?
    Ah OK. How about http://www.javaprogrammingforums.com...eamreader.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Location
    kolkata, India
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pig Latin Translator

    hey BMN,

    it seems u are using a static function "translate" from the "PigLatin" Class. ami correct? if thats the case then you shud kno that u need to have both the classes in the same project for this to work. if your school computer does not have the "PigLatin" class present then the code will not work. the best thing to do in this case is ofcourse what the admin has suggested.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Pig Latin Translator

    I've updated my example for you to include the InputStreamReader instead of the Scanner class.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class PigLatin {
     
        /**
         * JavaProgrammingForums.com
         */
     
        public static String translate(String sentance) {
     
            // Translate code here
            return sentance;
        }
     
        public static void main(String[] args) {
     
            PigLatin pig = new PigLatin();
     
            String sentence, result;
     
            try {
                InputStreamReader is = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(is);
                do {
                    System.out.println("Please enter a sentence: ");
                    sentence = br.readLine();
                    result = pig.translate(sentence);
                    System.out.println("That sentence in Pig Latin is: ");
                    System.out.println(result);
                } while (!result.equals("y"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Tags for this Thread