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

Thread: nested do while loop

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question nested do while loop

    I am very new at java and just trying to learn it on my spare time and I'm doing do-while loops and I am having trouble understanding the below code if somebody could help me out that would be great.

    public class help {
    public static void main (String args[])
    throws java.io.IOException {
    char choice, ignore;
    do{
    System.out.println ("Choose one:");
    System.out.println("1. if");
    System.out.println("2.switch");
     
    choice = (char) System.in.read();
    do{
    ignore = (char) System.in.read();
    }while(ignore != '\n');	
    }while (choice < '1'| choice >'2');
     
    switch(choice){
    case '1': 
    System.out.println("if");
    break;
    case '2':
    System.out.println("switch");
    break;
    }
    }
    }

    Whats the point of the block?
    do{
    ignore = (char) System.in.read();
    }while(ignore != '\n');

    It makes no difference in the program wither i delete this block or keep it

    Also can somebody explain how while (choice < '1'| choice >'2'); translates? I would assume it would be while (choice >= '1'| choice =<'2');?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,601
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: nested do while loop

    It makes no difference in the program wither i delete this block or keep it
    Please explain what "difference" you are expecting.

    The normal OR operator is || not |.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nested do while loop

    I'm not sure, I am just following a book and they said that that block get rids of unwanted characters? I dont see how it does that.

    please explain things to me in simple terms, I am a mechanical engineer, I have little to no experience with coding.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,601
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: nested do while loop

    I was asking a question because I did not understand your post.
    It makes no difference in the program wither i delete this block or keep it
    What was the book's reason for having that code?

    The normally used boolean OR operator is || not |.
    See the tutorial for a definition for boolean operators: Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    while (choice < '1'| choice >'2')
    That would be true for numbers
    from minus infinity up to '1' - .0000000....1
    and for numbers from '2' + .0000000...0001 to infinity
    AND false for chars from '1' to '2'
    Last edited by Norm; April 5th, 2014 at 02:17 PM. Reason: Need to differentiate between chars and ints
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: nested do while loop

    With regards to the code block in question, you can see for yourself what it does by printing out "ignore", e.g.,
    ignore = (char) System.in.read();
    System.out.println("[" + ignore + "]");

    To find out whether it is functionally useful, run the program (with the println statement in), enter "301" and observe the output. Stop the program (by typing 1 or 2, or press Ctrl-C). Remove the code block. Re-run the program, enter "301", and observe the difference in the output.

Similar Threads

  1. nested do while loop
    By crogn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2014, 03:20 AM
  2. Nested For...each Loop?
    By melki0795 in forum Java Theory & Questions
    Replies: 11
    Last Post: January 29th, 2014, 08:36 AM
  3. nested for loop
    By tardis_ in forum Loops & Control Statements
    Replies: 4
    Last Post: April 18th, 2013, 01:11 PM
  4. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 09:43 AM
  5. Nested for loop
    By wolf_fcknHaley33 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 23rd, 2012, 08:49 AM

Tags for this Thread