-
February 16th, 2009, 03:02 PM
#1
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);
}
}
}
-
February 16th, 2009, 03:32 PM
#2
Re: stumped on do while loops
Originally Posted by txthrizzle
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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 16th, 2009, 03:38 PM
#3
Re: stumped on do while loops
heh.. well nothing is working.... if you compile the program it just errors out..
-
February 16th, 2009, 03:38 PM
#4
Re: stumped on do while loops
We cannot read minds here, but it looks like you need a sentinel.
-
February 16th, 2009, 03:42 PM
#5
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.
-
February 16th, 2009, 03:49 PM
#6
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.
-
February 16th, 2009, 05:08 PM
#7
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..
-
February 16th, 2009, 05:15 PM
#8
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.
-
February 16th, 2009, 05:48 PM
#9
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
-
February 16th, 2009, 06:09 PM
#10
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.
-
February 16th, 2009, 07:29 PM
#11
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...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|