Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2012, 03:36 PM   PM User | #1
Vayd
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Vayd is an unknown quantity at this point
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.
Vayd is offline   Reply With Quote
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,661
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
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:38 AM.


Advertisement
Log in to turn off these ads.