Results 1 to 2 of 2
  1. #1
    leeavital is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Help with a repeat

    I'm doing a pig Latin translator and everything works fine except for the while loop I'm using to repeat in the client program.

    Java Code:
    import java.util.Scanner;
    public class TheClient
    {
        public static void main(String[] args)
        {
            Scanner theScan = new Scanner(System.in);
            String repeat = "y";
            String englishText;
            while(repeat.equals("y"))
            {
                System.out.print("Please enter english text:");
                englishText = theScan.nextLine();
                PigLatin translation = new PigLatin(englishText);
                System.out.println("Translation: " + translation);
                System.out.print("Press y to repeat: ");
                repeat = theScan.next();
            }
    
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    A few things:

    (1) It helps to post code that others can actually compile and run. In your case this is easy because the client is a separate class.

    Java Code:
    //PigLatin translation = new PigLatin(englishText);
    String translation = "whatever";

    (2) Use System.out.println() so you can see what the values of variables are. This gives you some insight into what the scanner methods are doing.

    Java Code:
    System.out.print("Please enter english text:");
    englishText = theScan.nextLine();
    System.out.println("englishText=|" + englishText + "|");
    // ...
    System.out.print("Press y to repeat: ");
    repeat = theScan.next();
    System.out.println("repeat=|" + repeat + "|");

    Notice how the pipe symbols (|) are handy to mark off a string which might start or end with white space.

    (3) Actually describe your problem. "everything works fine except" says nothing. It's the "except" that we are interested in. What does happen? Describe actual and intended program behaviour. Even better, use the System.out.println() suggestion and describe under what circumstances variables end up with values that you don't intend or expect.

Similar Threads

  1. Go-back-N & Selective repeat protocol
    By Engineera in forum Networking
    Replies: 3
    Last Post: 01-14-2009, 11:18 PM
  2. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 06:19 PM
  3. Replies: 1
    Last Post: 07-02-2008, 02:15 PM
  4. nested <ui:repeat> - problem
    By hackerofcrackers in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-20-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •