errors

i got the following code (driver class):

import javax.swing.*;

publicclass SnakeEyes

{

publicstaticvoid main (String[] args)

{

String play, rolls, faces;

int snakeEyes = 0, rollNum = 0, facesNum = 0;

double num1, num2;

play = JOptionPane.showInputDialog(null,"do you want to play SnakeEyes (y/n)?");

do

{

try

{

rolls = JOptionPane.showInputDialog(null,"how many rolls do you want");

rollNum = Integer.parseInt(rolls);

if (rollNum <= 0)

JOptionPane.showMessageDialog(null,"ERROR: Number less that 0");

}

catch (NumberFormatException e)// ERROR HANDLING: INPUT NOT CORRECT

{

JOptionPane.showMessageDialog(null,"ERROR - Invalid Input: Input new values");

}

Die die1 =new Die();//6 sided die

try

{

faces = JOptionPane.showInputDialog(null,"how many faces do you want (2-20)");

facesNum = Integer.parseInt(faces);

}

catch (NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"ERROR - Invalid Input: Input new values");

}

Die die2 =new Die(facesNum);

for (int roll = 1; roll <= rollNum; roll++)//Loop for number of rolls

{

die1.roll();

num1 = die1.getFaceValue();

die2.roll();

num2 = die2.getFaceValue();

if (num1 == 1 && num2 == 1)//Snakes eyes

snakeEyes = snakeEyes + 1;//If snake eyes, add 1

}

JOptionPane.showMessageDialog(null,"Ratio: " + (double)snakeEyes/rollNum);

JOptionPane.showMessageDialog(null,"SnakeEyes: " + snakeEyes);

JOptionPane.showMessageDialog(null,"Number of rolls " + rollNum);

play = JOptionPane.showInputDialog(null,"do you want to play SnakeEyes (y/n)?");

}

while(play.equals("y"));

}

}

You see where i have put "if (rollNum <= 0)" and then the error message comes up....

How do i then after the user presses 'OK' that the user re inputs the number of rolls?

Thanks

[3614 byte] By [The_Onea] at [2007-11-26 14:27:57]
# 1
Loop while the input is not valid.~
yawmarka at 2007-7-8 2:21:43 > top of Java-index,Java Essentials,Java Programming...
# 2

Take all the code out of the main method

main(String[] args){

new SnakeEyes();

}

public SnakeEyes(){

For the while flag dont use the flag "y". you can use a constant boolean

which is a bit clearer.

static final boolean YES = true;

static final boolean NO = false;

TuringPesta at 2007-7-8 2:21:43 > top of Java-index,Java Essentials,Java Programming...
# 3

ok i created a loop:

do

{

try

{

rolls = JOptionPane.showInputDialog(null, "how many rolls do you want");

rollNum = Integer.parseInt(rolls);

while(rollNum <= 0)

{

JOptionPane.showMessageDialog(null, "ERROR: Number less that 0");

rolls = JOptionPane.showInputDialog(null, "how many rolls do you want");

rollNum = Integer.parseInt(rolls);

}

}

catch (NumberFormatException e) // ERROR HANDLING: INPUT NOT CORRECT

{

JOptionPane.showMessageDialog(null, "ERROR - Invalid Input: Input new values");

}

Die die1 = new Die();

Just one question... Where do i put my try and catch in that so if its not a number it asks to re input?

The_Onea at 2007-7-8 2:21:43 > top of Java-index,Java Essentials,Java Programming...
# 4
alright never mind... i got it.
The_Onea at 2007-7-8 2:21:44 > top of Java-index,Java Essentials,Java Programming...