prompting the user to enter a valid value in an applet

hi everyone!

i have this code and i want to prompt the user to set a value<1000.

this doesnt work.if i enter a value <1000 the input dialog appears again.

whats wrong?

publicvoid init()

{

String j = JOptionPane.showInputDialog(null,"Enter a number less than 1000 but greater than 0");

int i = Integer.parseInt(j);

while(i>1000||i<1)

{

String k = JOptionPane.showInputDialog(null,"Enter a number less than 1000 but greater than 0");

int y = Integer.parseInt(k);

}

....code executedwhile i<1000 or i>0

[932 byte] By [dimitir_jpa] at [2007-10-3 2:34:57]
# 1

Your while is looping on i, but inside you're setting y not i. This should work:

do {

String k = get input from option pane

int i = Integer.parseInt(k);

} while (i > 1000 || i < 1);

BinaryDigita at 2007-7-14 19:33:53 > top of Java-index,Java Essentials,New To Java...
# 2

> Your while is looping on i, but inside you're setting

> y not i. This should work:

>

> > do {

>String k = get input from option pane

> int i = Integer.parseInt(k);

> } while (i > 1000 || i < 1);

>

In BinaryDigit's code, you won't have access to "i" after the loop. Declare it outside the loop (as you had it).

> int i = 0;

do {

String k = get input from option pane

i = Integer.parseInt(k);

} while (i > 1000 || i < 1);

MLRona at 2007-7-14 19:33:53 > top of Java-index,Java Essentials,New To Java...
# 3
yes now it works. but i wonder why i have no access to i if i declare it into the do-while statement?
dimitir_jpa at 2007-7-14 19:33:53 > top of Java-index,Java Essentials,New To Java...
# 4
> yes now it works. but i wonder why i have no access to i if i declare it into the do-while statement?If you declare "i" inside the do-while statement, the scope ends when the do-while ends. The scope of a variable declared after a { somewhere ends at the matching }.
MLRona at 2007-7-14 19:33:53 > top of Java-index,Java Essentials,New To Java...