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

Thread: Would you use a do-while for the following....?

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Would you use a do-while for the following....?

    What's up everybody? Ron here. I'm a n00b. Signed up just now. I hope everyone is doing alright. I would like a little advice from anyone who doesn't mind. I've been assigned the following program:

    Write a program that prompts the user for their name and then continually prompts the user for numbers until a sentinel value of -99 is entered. Your program should display the user's name along with the smallest number entered.

    I've pretty much built the framework using a do-while loop, but am now experiencing problems. Not sure if it's because I'm going in the wrong direction, or because I'm heading toward the end of a long day. If you were me, would you employ a do-while loop, or is there a better way?

    Because the class I'm in has only discussed for, while, and do-while loops, I assume I should work with one of those methods at this point.

    Thanks guys.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Would you use a do-while for the following....?

    Can you post your code?

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Sure thing. This is what I've done so far...

    import java.util.Scanner;

    public class Sentinel

    {
    public static void main (String[] args)
    {


    String name;
    int number;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please tell me your name.");
    name = keyboard.nextLine();
    System.out.println("Please enter a number " + name + ". " + "Or simply enter -99 if finished.");
    number = keyboard.nextInt();

    do
    {
    System.out.println("Ok " + name + ", " + "you entered " + number + ".");
    System.out.println("Please enter another number, or simply enter -99 if you are finished.");

    }
    while (number != -99);
    {
    System.out.println("Ok " + name + ". " + "Do enjoy the rest of your day.");
    }
    }
    }


    Crude, yes. Looping infinitely.

  4. #4
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Can you post your code so that we can look over what you have already done?
    now experiencing problems
    Can you elaborate upon this? What error messages are you getting?
    --------------------------------------------------------------------
    Whoops, beat me to the punch, lol!

    --- Update ---

    Crude, yes. Looping infinitely.
    To break the infinite loop, you want to add this:
     
    while (number != -99);
    {
    System.out.println("Ok " + name + ". " + "Do enjoy the rest of your day.");
    break; //<-----This
    }
    }
    }

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    First off, you guys are fast, and I thank you for your attention. Anyway, well, other than the infinite loop, which you've just showed me how to fix. (I'd known about the break statement, just didn't occur to me to apply it. I am a n00b after all.) I've also noted that when I do enter the sentinel number, I still have it coded to ask again if the user would like to enter another number. That's just something I need to remove of course.

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    If you break the problem down into small steps, then the problem will not seem nearly as complex.

    This
    continually prompts the user for numbers until a sentinel value of -99 is entered
    is going to be your loop.

    In psuedocode:
    do {
    //prompts user to enter a number
    }
    while (number != -99) {
    //end program
    }

    Take a look at this diagram to understand what exactly a do-while loop is: Fileo-while-loop-diagram.svg - Wikipedia, the free encyclopedia

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Looking at the loop now diagram now. Also, is there a convention for posting code in this forum, i.e., like a <code> </code> sort of thing? I'm using jGrasp btw. Thanks Blasfemmy for your help.

  8. #8
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    The main differences between the three loops that you described are:
    A "do-while loop" checks to see if the condition is true or false after the code is executed.
    A "while loop" checks the condition before the code is executed. It will run as long as the condition is true, and then terminates when it is false.
    A "for loop" will often be seen with a counter variable. This specific loop allows for a certain block of code to be executed a predefined number of times.

    --- Update ---

    It's no problem, I've been there before, lol! If you switch the reply mode to "Advanced Mode", then there will be a spot that adds in code tags. I just add them in manually though. To start a block of code, just type the word "code" inside of brackets, then to end it, do the same thing, except add a slash (/) before the word code.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Ahh, I see. OK.

    --- Update ---

    Just for kicks, I tried it with an if-else structure. jGrasp no likey. Says 'error: else without if'.

    import java.util.Scanner; 
     
    public class Sentinel
     
    {
    public static void main (String[] args)
    {
     
     
    String name;
    int number;
     
    Scanner keyboard = new Scanner(System.in);
     
    System.out.println("Please tell me your name.");
    name = keyboard.nextLine();
    System.out.println("Please enter a number " + name + ". " + "Or simply enter -99 if finished.");
    number = keyboard.nextInt();
     
    if (number != -99);
    	{
    		System.out.println("Ok " + name + ", " + "you entered " + number + ".");
    		System.out.println("Please enter a number " + name + ". " + "Or simply enter -99 if finished.");
    	}
    else	
    	{
    		System.out.println("Ok " + name + ". " + "Do enjoy the rest of your day.");
    	}
    }
    }

  10. #10
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Your error lies here:
    if (number != -99);
    It should be
     if(number != -99) {
    Keep in mind that the user needs to be continuously prompted (loop) with the questions for entering a number. What this does is ask the question and then check to see if it is -99, then end the program.

  11. The Following User Says Thank You to Blasfemmy For This Useful Post:

    curmudgeon (February 9th, 2013)

  12. #11
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Right, right. (The semicolon.) I noticed that last night too. But I've since decided to go back to a standard while loop. In my n00b mind, that really seems the way to go.

    You also point out that at this point, the program simply ends after testing the condition. Yep, yep, yep. That's one of two things I'm trying to sort out in my mind. I can resolve it. But I need to devote a minute or two to this program and nothing else. (I'm hopping back and forth between classes and not concentrating on this issue alone. Which tonight, I think I should be able to do.)

    The other thing I'm trying to resolve in my head while I'm doing other things is how exactly to get the program to to eventually return the lowest value the use enters. And I think I just figured it out while I was typing that. (That's what sleep will do for ya.) Anyway, I think I recall reading about a MIN_VALUE
    method. Probably gunna read up on that and try it. I'll post some new stuff tonight and hopefully do you proud.

  13. #12
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Aye yai yai. This is getting old. I reworked the code. Still having issues. Pretty much the same ones. I didn't expect this current version to be perfect. Still haven't figured out how I'll get the lowest number entered. But I DID think I'd get the infinite looping resolved. I'll have it fixed tomorrow.

    import java.util.Scanner; 
     
    public class Sentinel
     
    {
    public static void main (String[] args)
    {
     
     
    String name;
    int number;
     
    Scanner keyboard = new Scanner(System.in);
     
    System.out.println("Hello. What is your name?");
    name = keyboard.nextLine();
    System.out.println("Nice to meet you " + name + ".");
    System.out.println("I will ask you to enter some numbers. Enter -99 when you are finished.");
    number = keyboard.nextInt();
    System.out.println("OK " + name + ", enter any number you like, or -99 to quit.");
     
    while (number != -99)
    {
    	System.out.println("Enter another number, or -99 if finsihed.");
    	break;
    }
     
    	System.out.println("Very well " + name + "." + "The lowest number you entered was.");
    	System.out.println("Have a nice day.");
     
    }
    }

  14. #13
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    I am at a loss here. I really think this should work, and for the life of me I cannot see why it doesn't. It looks to be in working order to me. But it's still looping infinitely. Still. Anyone have a suggestion?


    import java.util.Scanner; 
     
    public class Sentinel
     
    {
    public static void main (String[] args)
    {
     
     
    String name;
    int number;
    int smallNum = 0;
     
    Scanner keyboard = new Scanner(System.in);
     
    System.out.println("Hello. What is your name?");
    name = keyboard.nextLine();
    System.out.println("Nice to meet you " + name + ".");
    System.out.println("I will ask you to enter some numbers. Enter -99 when you are finished."); //It snags up here first.
    System.out.println("OK " + name + ", enter any number you like, or -99 to quit."); //This line is ignored.
    number = keyboard.nextInt();
                                              //So I enter a number anyway, if not -99, infinite loop.
    while (number != -99)							// I just don't get it.
    	{
    	System.out.println("Enter another number, or -99 if finsihed.");
     
     
     
    	if (smallNum > number && number != -99)
    	{
    	smallNum = number;
    	}
     
    	}
     
    	System.out.println("Very well " + name + "." + "The lowest number you entered was " + smallNum + ".");
    	System.out.println("Have a nice day.");
     
    }
    }

  15. #14
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Would you use a do-while for the following....?

    .

  16. #15
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Would you use a do-while for the following....?

    Wow. Thanks for the step-by-step. I will re-order my code so that it is line with it and re-post it later this evening. Thanks JIC.

  17. #16
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Would you use a do-while for the following....?

    .