This site requires JavaScript, please enable it in your browser!
Greenfoot back
decadence18
decadence18 wrote ...

2015/4/10

How can I add actors while the world is running?

1
2
danpost danpost

2015/4/10

#
decadence18 wrote...
Up until this point in our code, we were mostly comparing integer values so I would just convert them with parseInt and then compare them to make sure the value isn't null or empty.
You should check for 'null' and empty before trying to parse the returned string. Even better, since if the entered string cannot be parsed, you will throw a NumberFormatException error, you could use code similar to the following to catch any and all problems (including 'null' and empty):
numberOfActors = 0;
howManyActors= JOptionPane.showInputDialog("How many actors?");
try
{
    numberOfActors = Integer.parseInt(howManyActors);
}
catch(NumberFormatException nfe)
{
    JOptionPane.showMessageDialog(null, "Invalid numeric entry");
}
decadence18 decadence18

2015/4/10

#
danpost wrote...
decadence18 wrote...
Up until this point in our code, we were mostly comparing integer values so I would just convert them with parseInt and then compare them to make sure the value isn't null or empty.
You should check for 'null' and empty before trying to parse the returned string. Even better, since if the entered string cannot be parsed, you will throw a NumberFormatException error, you could use code similar to the following to catch any and all problems (including 'null' and empty):
numberOfActors = 0;
howManyActors= JOptionPane.showInputDialog("How many actors?");
try
{
    numberOfActors = Integer.parseInt(howManyActors);
}
catch(NumberFormatException nfe)
{
    JOptionPane.showMessageDialog(null, "Invalid numeric entry");
}
That's essentially what we're doing first, only we're using if statements to check if it's null, if isEmpty() is true, and then using a for statement on an array to check each value for non-numeric characters. Thank you for your help with this Danpost (and others)
You need to login to post a reply.
1
2