java

John R. Ghidiu john at rmdashrf.org
Thu Sep 4 05:35:51 EDT 2003


On Wed, 3 Sep 2003, Riga, Anthony wrote:

> Anyone good at java? I am running Borland JBuilder9 on Redhat 9.0. I
> need to write a program that uses JOptionPane class. You enter 3 numbers
> and the program computes the sum,porduct and avg of all type int as well
> as the average number of type double. Any Ideas? Just get me started.
> Thanks

Well, I wouldn't use a JOptionPane for that (unless you are required to).
The option pane was not really built for that (which is not to say that
you can't do it). I would just use a JFrame with three JTextFields, three
JLabels (for the results) and a JButton for starting the calculations.
Your best bet is probably to build the GUI in the form builder, double
click on the button when you are done - this will place your cursor in the
code where the button handler is. From here, just do something like:

JLabel1.setText(/* code for sum /*);
JLabel2.setText(/* code for product  */);
JLabel3.setText(/* code for average */);

Assuming, of course, that JLabel1, JLabel2 and JLabel3 are the labels in
the GUI.

In general, you can get the integer representation of a String (which is
what JTextFields contain) by doing something like:

int someValue = Integer.parseInt(someString);

Where someString is a string. Be aware that a RuntimeException might be
thrown when this method is called.

So, to get the average, you might do something like:

int average = (Integer.parseInt(JTextField1.getText()) +
               Integer.parseInt(JTextField2.getText()) +
               Integer.parseInt(JTextField3.getText()))/3;

Where JTextField1, JTextField2 and JTextField3 are the text fields on the
form.

Hope that helps

-- 
John R. Ghidiu
john at rmdashrf.org

"Just don't create a file called -rf. :-)"
  - Larry Wall in <11393 at jpl-devvax.JPL.NASA.GOV>




More information about the nflug mailing list