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 3 of 3

Thread: Simple Nested Do/While Loop

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Nested Do/While Loop

    I was wondering if you can help me out with this code, I am learning on my own mostly from a book.
    The code runs fine but in the 2nd do/while statement I am curious about the condition: (ch=='\n') or (ch=='\r'); Why would ch be equal to a carrage return or line feed? I found out that line feed and carriage returns occur when a keyboard character is input, but do they occur continuously? Why not use the condition: while(ch != 'K'); this condition should run the loop until 'K' is selected on the keyboard, shouldn't it?

    Thanks in advance-Farmer

    public class HSGuess4 {
    public static void main(String[] args) throws java.io.IOException{
    char ch, answer = 'K';

    do{
    System.out.println("I'm thinkning of a letter between A and Z ");
    System.out.println("Can you guess it: ");

    //read a letter, but skip cr/lf (carrage return and line feed)
    do{
    ch = (char) System.in.read(); // get char from keyboard command
    } while(ch == '\n'| ch == '\r');

    if(ch == answer) System.out.println("**Right**");

    else{
    System.out.println("Sorry you're ");
    if(ch < answer) System.out.println("too low");
    else System.out.println("too high");
    System.out.println("Try again!\n");
    }
    } while(answer !=ch);
    }
    }


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Simple Nested Do/While Loop

    The first time through, there shouldn't be any cr/lf characters, but after each incorrect answer, it retries by going round the loop again, so the cr/lf from the previous entry is still waiting to be read.

    This is one disadvantage of using a 'raw' input stream. Normally one would wrap System.in in a Scanner or some other convenient higher-level input class.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Nested Do/While Loop

    dlorde,

    Thanks for you input. I have seen the Scanner class used but have not tried it yet. I am just working my way through text book examples. Again your feedback was helpful, thank you.

Similar Threads

  1. The defitition of nested loop
    By silentbang in forum Java Theory & Questions
    Replies: 2
    Last Post: October 16th, 2010, 02:38 PM
  2. do while loop with nested while question
    By johneusmc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2010, 04:45 PM
  3. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM
  4. Equilateral Triangle Using nested for loop
    By uchizenmaru in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 10:01 PM
  5. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM