Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •