CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    7

    stumped on do while loops

    Having a little trouble creating a program.. Well to start I need to create a program that will list all of the numbers between two input values, in either ascending or descending order. The user will enter a lower bound and upper bound number through a scanner object, and enter whether they want Ascending or Descending via A/a or D/d. if the user enters an incorrect A/a or D/d the user will be stated error or what not.. but here is my code.. any help would be greatly appreciated. thank you.
    Code:
    import java.util.Scanner;
     
    public class assign4
    {
     
    	public static void main(String[] args)
    	{
    		int low, up;
    		Scanner input = new Scanner(System.in);
    		String A, a, D, d, check;
     
    	do
    	{
    		System.out.print("Enter lower bound integer: ");
    		low = input.nextInt();
    		System.out.print("Enter upper bound integer: ");
    		up = input.nextInt();
    		System.out.println("Lower bound: " + low);
    		System.out.println("Upper bound: " + up);
    	}
    	while (low <= up);
    	{	
    		
    			
    		System.out.println("Error!! lower bound must be smaller.");
    	}
    	
    	
    		System.out.print("Would you like Ascending or Descending order? [A/a or D/d]: ");
     
    		A = input.check;
    		a = input.check;
    		D = input.check;
    		d = input.check;
    		
    	while (check.equals("A,a"));
    	{
    		System.out.println(low + 1);
     
    	}	
    	while (check.equals("D,d"));
    	{
    	System.out.println(top - 1);
    	}
     
    }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: stumped on do while loops

    Quote Originally Posted by txthrizzle View Post
    Having a little trouble creating a program..
    any help would be greatly appreciated. thank you.
    So what is the problem exactly?

    A prudent question is one-half of wisdom...
    F. Bacon
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jan 2009
    Posts
    7

    Re: stumped on do while loops

    heh.. well nothing is working.... if you compile the program it just errors out..

  4. #4
    Join Date
    Sep 2006
    Location
    Wantagh,NY
    Posts
    151

    Re: stumped on do while loops

    We cannot read minds here, but it looks like you need a sentinel.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: stumped on do while loops

    If you tell us what your problem actually is then you will get much better help.

    Having had a quick glance at your code I've noticed the following:

    Code:
    		System.out.print("Would you like Ascending or Descending order? [A/a or D/d]: ");
     
    		A = input.check;
    		a = input.check;
    		D = input.check;
    		d = input.check;
    What is this supposed to do?

    Code:
    	while (check.equals("A,a"));
    This will test if the variable 'check' (which you don't have) equals "A,a" and not "A" or "a". If you want to test for the latter use the equalsIgnoreCase() method.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: stumped on do while loops

    heh.. well nothing is working.... if you compile the program it just errors out..
    Err well fix the errors then.

    The compiler gives fairly good descriptions of what each error is. Start with the first one and work your way down the list of errors. If you get to an error that doesn't make sense then try recompiling as if there are serious errors the compiler may be unable to make sense of the code after the error and may generate some spurious output.

    If you can't understand an error message then post the latest code and the error message and someone will explain it to you. Just saying it won't work won't get you much sympathy or help.

  7. #7
    Join Date
    Jan 2009
    Posts
    7

    Question Re: stumped on do while loops

    Code:
    	
    		check = System.out.print("Would you like Ascending or Descending order? [A/a or D/d]: ");
     
    		A = input.check;
    		a = input.check;
    		D = input.check;
    		d = input.check;
    		
    	while (check.equals("A,a"));
    	{
    		System.out.println(low + 1);
     
    	}	
    	while (check.equals("D,d"));
    	{
    	System.out.println(top - 1);
    	}
     
    }
    }
    I added the check =, for the check variable..

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: stumped on do while loops

    I added the check =, for the check variable..
    Sorry but that makes no sense to me.

    As I said before you don't have a check variable - you haven't declared one in your code and the scanner class doesn't have one so that's two erroneous uses of that variable name.

    Please answer the question I asked earlier so I can understand what you are trying to do.

  9. #9
    Join Date
    Jan 2009
    Posts
    7

    Re: stumped on do while loops

    public static void main(String[] args)
    {
    int low, up;
    Scanner input = new Scanner(System.in);
    String A, a, D, d, check;

    thats where my variable is stated

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: stumped on do while loops

    Sorry, I missed that, but the scanner class definitely hasn't got a check variable which leads once again back to the question I asked several posts ago ie what is the following code supposed to be doing:
    check = System.out.print("Would you like Ascending or Descending order? [A/a or D/d]: ");

    A = input.check;
    a = input.check;
    D = input.check;
    d = input.check;
    And whilst I'm repeating myself post the error messages if you can't fix them yourself.

  11. #11
    Join Date
    Feb 2009
    Posts
    32

    Re: stumped on do while loops

    If you don't understand basic syntax of Java, use google; don't use a forum. I'm going to help you anyway...
    Code:
    A = input.check;
    This is unintelligible. There is no check instance variable inside the Scanner object input that you can access.
    Code:
    	do
    	{
    		System.out.print("Enter lower bound integer: ");
    		low = input.nextInt();
    		System.out.print("Enter upper bound integer: ");
    		up = input.nextInt();
    		System.out.println("Lower bound: " + low);
    		System.out.println("Upper bound: " + up);
    	}
    	while (low <= up);
    	{	
    		
    			
    		System.out.println("Error!! lower bound must be smaller.");
    	}
    A do-while loop follows the basic syntax:

    do {
    ... statement(s) ...
    } while (boolean expression);

    Where the statement(s) are execute, THEN the boolean expression is checked. If the expression is true, then the statement(s) execute AGAIN. This is helpful only when you want to execute a set of statements within a group AT LEAST ONCE.

    if (b) {
    do {
    ... statement(s) ...
    } while (b);
    }

    is equivalent to

    while (b) {
    ... statement(s) ...
    }

    where b is some boolean expression. Hopefully that helps you understand the do-while, since your title indicates to me that that is why you began this post.

    Hopefully you will direct your further basic syntactical questions to google, since it would be faster for you.
    Last edited by Nim; February 16th, 2009 at 07:37 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured