View Single Post
Old 10-04-2012, 06:53 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I won't go through the code you have here exactly, rather just go over the JOptionPane.

The method you want for input is JOptionPane.showInputDialog. This is always a string, so in order to capture it as valid data:
PHP Code:
                int starting = -1;
                
boolean bValid false;
                do
                {
                    try
                    {
                        
Integer input Integer.parseInt(JOptionPane.showInputDialog("Enter starting population of organisms: "));
                        
starting input.intValue();
                        
bValid true;
                        
// whatever other validation you need here, can throw an exception (change exception below if you do).
                    
}
                    catch (
NumberFormatException ex)
                    {
                        
JOptionPane.showMessageDialog(null"Invalid input!""Error"JOptionPane.ERROR_MESSAGE);
                    }
                }
                while (!
bValid); 
And so forth.

BTW, you can easily combine the swing and the system.out. That's not an issue, but it does mean if you want to see it you have to launch it from the command line as you won't see the messages unless you redirect them elsewhere.
Fou-Lu is offline   Reply With Quote