Loop problem

Can some kind person help me with this loop?

The user needs to input a number between 5 and 15. It needs to loop until they enter a correct number.

The error is noted in the code.

Thanks,

publicclass Five{

publicstaticvoid main( String[ ] args ){

int choice = 0;

String input;//error: local variable input is never read

while (choice >= 5 || choice <= 15);

{

input = JOptionPane.showInputDialog("Enter number between 5 and 15");

if (choice >= 5 && choice <= 15)

// correct response

JOptionPane.showMessageDialog(null,"You got it right!" );

{

// incorrect response

JOptionPane.showMessageDialog(null,"Nice try! Attempt was incorrect...Try again" );

}

}

}

}

[1450 byte] By [Fedjea] at [2007-10-3 8:29:31]
# 1

Draw a number line.

Use it to indicate >= 5. (greater than or equal to 5)

Now indicate <= 15. (less than or equal to 15)

|| evaluates to true if at least one of its operands is true. So as long as a number is on at least one of those parts of the number line you indicated, your while condition will be true.

Can you tell me a number that is both NOT >= 5 AND ALSO NOT <= 15? This is the kind of number you need for the while to be false.

In addition, you have this:

while (...) ; { /* stuff */ }

That's the same as

while (...) { /* nothing */ } { /* stuff */ }

Get rid of the semicolon after the while condition.

jverda at 2007-7-15 3:36:23 > top of Java-index,Java Essentials,New To Java...
# 2

First of all, you have a semi-colon after your while. Look at this:

String input = JOptionPane.showInputDialog("Enter number between 5 and 15");

int choice = Integer.parseInt(input);

while(choice > 15 || choice < 5)

{

//redo

}

CaptainMorgan08a at 2007-7-15 3:36:23 > top of Java-index,Java Essentials,New To Java...
# 3
The error means exactly what it says: You assign a value to input, but you never use it. Presumably you want to parse input into choice.
Mr_Evila at 2007-7-15 3:36:23 > top of Java-index,Java Essentials,New To Java...
# 4
cross posted.
7studa at 2007-7-15 3:36:23 > top of Java-index,Java Essentials,New To Java...
# 5

> String input;//error: local variable input is

> s never read

That's not an error. It's just a warning. Your code will still compile. It's legal, but the compiler has noticed that something's fishy. Like the error message says--you're never using that variable. There's no point to have a variable if you never use its value.

jverda at 2007-7-15 3:36:23 > top of Java-index,Java Essentials,New To Java...
# 6
> cross posted.It was? Where?
CaptainMorgan08a at 2007-7-15 3:36:23 > top of Java-index,Java Essentials,New To Java...
# 7

I think he meant my code on the cross post comment. I never put the question anywhere else.

I removed the semi colon after the while statement. Your right it does compile, but never ends because any number I put in will give the 'try again' return, which says it's wrong, even if it's not!

Dang I'm getting a headache!

Fedjea at 2007-7-15 3:36:23 > top of Java-index,Java Essentials,New To Java...
# 8
if (choice >= 5 && choice <= 15)// correct responseJOptionPane.showMessageDialog(null, "You got it right!" );Look at your logic here.
CaptainMorgan08a at 2007-7-15 3:36:24 > top of Java-index,Java Essentials,New To Java...
# 9
You really dont even need an if-statement inside your while loop. Read a String before the loop, and keep looping until a correct response is entered, then show the correct response message dialog after the loop.
CaptainMorgan08a at 2007-7-15 3:36:24 > top of Java-index,Java Essentials,New To Java...
# 10

This makes the most sense after I get the whole statement through my dyslexic brain. I'll have to break it down into parts...

"Read a String before the loop"

String input;

input = JOptionPane.showInputDialog("Enter number between 5 and 15");

"keep looping until a correct response is entered"

while (choice >= 5 && choice <= 15)

"then show the correct response message dialog after the loop"

JOptionPane.showMessageDialog

Then do this again for the incorrect answer? Nope, tried that while I was writing this. I get the answer is correct when it's not.

I'm tired and know this should be simple! Probably in wrong order or some dumb thing.

Here is where I'm at now...

import javax.swing.*;// provides JOptionPane class

public class Five{

public static void main( String[ ] args ) {

int choice = 0;

String input;

input = JOptionPane.showInputDialog("Enter number between 5 and 15");

// correct response

JOptionPane.showMessageDialog(null, "You got it right!" );

while (choice >= 5 && choice <= 15)

{

// incorrect response

JOptionPane.showMessageDialog(null, "Nice try! Attempt was incorrect...Try again" );

while (choice < 5 && choice > 15);

}

}

}

Fedjea at 2007-7-15 3:36:24 > top of Java-index,Java Essentials,New To Java...
# 11
while (choice >= 5 && choice <= 15)You do not want your loop to be like this. That would be a correct answer. You want it to loop if choice is less than 5 or if choice is greater than 15.
CaptainMorgan08a at 2007-7-15 3:36:24 > top of Java-index,Java Essentials,New To Java...
# 12
Yes I did see that after I posted. Changed it to this... while (choice < 5 && choice > 15) and tried this.... while(choice<5 || choice >15) still gives them as all correct answers!Message was edited by: Fedje
Fedjea at 2007-7-15 3:36:24 > top of Java-index,Java Essentials,New To Java...
# 13

So you have something like this:

String input;

input = JOptionPane.showInputDialog("Enter number between 5 and 15");

int choice = Integer.parseInt(input);

while(choice < 5 || choice > 15)

{

//incorrect response

//redo

}

//correct response

And its not working?

CaptainMorgan08a at 2007-7-15 3:36:24 > top of Java-index,Java Essentials,New To Java...
# 14

> Yes I did see that after I posted. Changed it to

> this... while (choice < 5 && choice > 15)

This will be true for every number that is both less than 5 and greater than 15. Can you name a number that's less than 5 and greater than 15?

If you're dyslexic, you really should try some other means to help you visualize the meaning of the string of tokens. For example, what you have above could be like this:

< 5 <|

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

>15|>

&& means any number that meets both of those conditions--that is, falls on both of those lines. No number matches that, so the while condition can never be true;

For your initial x >= 5 || x <= 15, you have this:

>= 5|>

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

<= 15<|

|| means at least one of those conditions is met, so the while condition is true for any number that's on at least one of those lines. Every number is on at least one of them, so it's always true.

> this.... while(choice<5 || choice >15) still gives

> them as all correct answers!

That sounds fishy. I'd have to see more code.

while (choice < 5 || choice > 15) {

// tell them the choice is wrong, must bet between 5 and 15, inclusive

}

< 5<--|

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

> 15|-->

|| means keep doing the "you got it wrong" part as long as the number is on at least one of those lines. So "you got it wrong" is everything except the blank space between 5 and 15.

jverda at 2007-7-15 3:36:24 > top of Java-index,Java Essentials,New To Java...
# 15
I also see two while loops in your most recent code. That doesn't make sense.
jverda at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 16
> I never put the question anywhere else.Funny, you seem to have the exact same syntax problems with if statements and while loops as a Ms. Linda Guy. Same class name as well.
7studa at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 17
> > I never put the question anywhere else.> > Funny, you seem to have the exact same syntax> problems with if statements and while loops as a Ms.> Linda Guy. Same class name as well.Link?
jverda at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 18

Sorry about the lapse in this, I had a computer problem.

This helps to understand a lot! Thanks!

I decided to try this as an "if/else" It almost works, but still gives the wrong answer, they're reversed! If I put in 3 it says right, put in 7 and it says wrong. I tried switching the output, still the same.

import javax.swing.*;// provides JOptionPane class

public class FivetoFifteen {

public static void main(String[] args) {

String input;

input = JOptionPane.showInputDialog("Enter number between 5 and 15");

int choice = Integer.parseInt(input);

if (choice <= 5 || choice >= 15){

// correct response

JOptionPane.showMessageDialog(null, "You got it right!" );

} else {

// incorrect response

JOptionPane.showMessageDialog(null, "Nice try! Attempt was incorrect...Try again" );

}

}

}

Fedjea at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 19
if (choice <= 5 || choice >= 15)Your logic is wrong here. The answer is right if choice is greater than 5 and less than 15.
CaptainMorgan08a at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 20

This is the only thread I have started on this forum, am I registered twice and making two threads? If so, I'm sorry.

I had tried another forum with no answers to questions or help given, and came here.

I have to say I have had better results on this thread! I understand a lot better what I'm doing, and why!

The chart with the lines helped a lot, and I will use that in the future. My final problem is why it is reversed, sounds like my dyslexia at work, but I tried reversing things in case that was the problem with the same results.

Fedjea at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 21

Original Message:

Re: Loop problem

CaptainMorgan08 Registered: May 10, 2006 8:31 AM Oct 29, 2006 9:55 AM

if (choice <= 5 || choice >= 15)

Your logic is wrong here. The answer is right if choice is greater than 5 and less than 15.

I tried changing it toif (choice >=5 || choice <=15) and get the same result.

Correction: It says 'correct' for both that way.

Message was edited by:

Fedje

Fedjea at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 22
> I tried changing it toif (choice >=5 || choice<=15) and get the same result.Thats says "if choice is greater than 5 or less than 15." This is always true. Change the 'or' to an 'and.'
CaptainMorgan08a at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 23
Okay, I changed the output statements around and now all are 'not correct.' It seems my 'else' statement is not working, did I do it right? I followed the tutorial, and show no errors.
Fedjea at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 24
Keep the outputs like the were when you last posted your code. Just change the if statement to "if choice is greater than 5 and choice is less than 15."
CaptainMorgan08a at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 25

I finally got it!!!!!! Dang dysleyxia! Yippie!! Yippie!! Yippie!!

I see I have to draw a chart for things like this.

Two of you really helped me a lot, can I add more Duke Dollars and divide them between you? ;>)

Yes, I see I could do this and they have been awarded! Thanks!

Message was edited by:

Fedje

Fedjea at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 26
> Thanks!No problem.
CaptainMorgan08a at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 27
>I had tried another forum with no answers to questions or help givenAnother lie.
7studa at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 28

AH HA!! Your the one told to be nice on the other site! Personally I couldn't understand WHAT you were trying to tell me to do.

If you read through this thread you will see how to help a noobie. After all, we're very stupid until we learn. I really envy your knowledge, but until I learn this...that is all I can do is envy you.

Fedjea at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 29

CaptainMorgan08...You would be proud of me!

I changed this to a while loop so it will repeat until they put in a correct number.

It only repeats once right now, then closes, but I will find how to make it do what I want!

Probably just needs some little piece of code to do it.

import javax.swing.*;// provides JOptionPane class

public class Five{

public static void main( String[ ] args ) {

String input;

String wrong;

input = JOptionPane.showInputDialog("Enter number between 5 and 15");

int choice = Integer.parseInt(input);

while (choice >= 5 && choice <= 15)

// correct response

JOptionPane.showMessageDialog(null, "You got it right!");

{

// incorrect response

wrong = "";

JOptionPane.showMessageDialog(null, "Nice try! Attempt was incorrect...Try again");

JOptionPane.showInputDialog("Enter number between 5 and 15");

}

}

}

Fedjea at 2007-7-21 12:28:08 > top of Java-index,Java Essentials,New To Java...
# 30

I don't like handing over code, but you've obviously been working on this and struggling with it, so here you go.

choice = ...;

while (choice < 5 || choice > 15) { // keep going until they enter a number between 5 and 15

error message "number has to be between 5 and 15

input = ...;

choice = ...;

}

// once we get here, they have finally entered a number between 5 and 15.

The way you're doing it now (in reply 29), you're going to continue asking for input as long as they're entering numbers between 5 and 15. I thought that what you wanted is to require a number between 5 and 15, and so you'd need to keep asking for input as long as they're entering numbers outside that range.

jverda at 2007-7-21 12:28:13 > top of Java-index,Java Essentials,New To Java...
# 31

FYI, here's a summary.

// #1 keep going until they enter a number between 5 and 15.

// That is, keep going as long as the number is outside that range.

while (choice < 5 || choice > 15)

// #2 keep going until they enter a number NOT between 5 and 15.

// That is, keep going as long as the number is inside the range.

// The exact opposite of #1.

while (choice >=5 && choice <= 15)

// #3 keep going until they enter a number that is both

// less than 5 and greater than 15

// This will never be true, since no single number is both < 5 and > 15

while (choice < 5 && choice > 15)

// #4 keep going until they enter a number that is either

// greater than or equal to 5 OR less than or equal to 15.

// This will loop forevever, because every number satisfies

// at least one of those conditions.

// The exact opposite of #3.

while (choice >= 5 || choice <= 15)

jverda at 2007-7-21 12:28:13 > top of Java-index,Java Essentials,New To Java...
# 32
>If you read through this thread you will see how >to help a noobie. I try to avoid helping cross posters.
7studa at 2007-7-21 12:28:13 > top of Java-index,Java Essentials,New To Java...