-2

im trying this code but it didn't work, the code should ask you for number again and agin until you enter a positive number. Sorry for my bad english, tnx !

System.out.print("Precio: ");
precio[contPre] = Double.parseDouble(teclado.readLine());

try {
    double precioCatch = Double.parseDouble(String.valueOf(precio[contPre]));
    if (precioCatch < 0) 
        throw new Exception();
}catch (Exception e) {
    System.out.println("Number must be >0");
    continue;
}
Morteza Asadi
  • 1,593
  • 2
  • 12
  • 33
midway
  • 1
  • 1
  • 1
    In order to do something again and again, you need to use some kind of loop (for, while). You have no loop in your code, so it will be executed only once. – TDG Jan 14 '17 at 11:44
  • Hi, Yes, you're right, I've already corrected it using if and while Tnx u soo much :-) – midway Jan 14 '17 at 12:00

1 Answers1

0

Thank you for the answers guys, i found the solution and it works:

precio[contPre] = Double.parseDouble(teclado.readLine());
if (0 > precio[contPre]) {
    while (0 > precio[contPre]) {
        System.out.print("Number must be a positive: ");
        precio[contPre] = Double.parseDouble(teclado.readLine());
    }
}
Nicolas Filotto
  • 39,066
  • 11
  • 82
  • 105
midway
  • 1
  • 1