CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Using Dialog Boxes in this Script (http://www.codingforums.com/showthread.php?t=275163)

Vayd 10-04-2012 03:36 PM

Using Dialog Boxes in this Script
 
I wrote this:

Code:

import java.util.*; //Needed for Scanner Class


public class population //Begin class Population

{
        public static void main (String[] args)
                                //Begin main function
                {
                //Variable declartion
                int starting,avg_daily, no_days;
                //create Scanner class for keyboard input
                Scanner keyboard = new Scanner(System.in);
                //Get a population
                System.out.println("Enter starting population of organisms: ");
                starting = keyboard.nextInt();
                //input validation
                if (starting<2)
                {
                //Display invalid messages
                System.out.println("Invalid Renter:");
                starting = keyboard.nextInt();
                }
                //Input average daily population
                System.out.println("Enter average daily population: ");
                avg_daily = keyboard.nextInt();
                if (avg_daily<0)//Validating daily percentage
                {
                        System.out.println("Invalid Renter: ");
                        avg_daily= keyboard.nextInt();
                }
                avg_daily=(avg_daily*starting)/100;
                //Inputting number of days
                System.out.println("Enter number of days: ");
                no_days = keyboard.nextInt();
                for(int i=0;i<no_days;i++)//loop repeats for all days
                {
                        System.out.println("Day " + (i+1) +":" + starting);
                        starting=starting+avg_daily;
                }
               
        }//End main function
}//end class Population

How would I write this so that it uses dialog boxes for the input questions? I know I would import javax.swing.JOptionPane; ... but if I put JOptionPane.showMessageDialog instead of System.out.println it will obviously give me errors, how do i fix that.

Fou-Lu 10-04-2012 06:53 PM

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.


All times are GMT +1. The time now is 08:19 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.