stumped on do while loops


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: stumped on do while loops

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    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
    Join Date
    May 2004
    Posts
    40

    stumped on do while loops

    Ok, your first loop is a do - while. That means the code between the do and the while will be executed while the condition is true. See the Java Tutorial for details.

    The code in brackets following your loop
    Code:
    	{	
    		System.out.println("Error!! lower bound must be smaller.");
    	}
    is not part of the loop and will be executed once each time the program is run. If you want it printed as an error message, placing it in an if statement inside the loop would be more appropriate.

    In this segment
    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;
    I'm not sure what you're doing. The input object is of class Scanner, which doesn't have a check method (scanner JavaDoc). Also, you don't really need to have a separate String object for each of the user choices; one object named "choice" or something similar would probably be good. Once you have the user input in choice (perhaps via input.nextLine()), you'd want to decide whether to do ascending or descending (if statement again?) based on that value. You can use toLower() to make your comparisons seem case insensitive.

    Your last two loops, should loop based on whether the two input numbers are equal. Make sure you actually increment or decrement the values as top - 1 and low + 1 only create a modified value, it is not assigned anywhere. The increment (++) and decrement (--) operators can help you here.

    If you don't really increment and decrement the values, your while loop will be endless and your program will never exit.

    Keep trying and post your questions here.

    Hope this helps,
    alan
    Last edited by ajhampson; 02-18-2009 at 02:57 PM. Reason: Fix typo.

Similar Threads

  1. Need help with while loops
    By Falcone in forum Java
    Replies: 6
    Last Post: 11-10-2007, 08:19 AM
  2. Totally code illiterate and stumped...please help
    By defyinggravity in forum AJAX
    Replies: 0
    Last Post: 02-07-2007, 05:35 PM
  3. while loops
    By rohan076 in forum Java
    Replies: 5
    Last Post: 03-29-2006, 07:15 PM
  4. Replies: 0
    Last Post: 06-15-2003, 05:10 PM
  5. HELP - ADO problem has me stumped.
    By J. Giudice in forum ASP.NET
    Replies: 0
    Last Post: 10-12-2000, 11:16 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center