I am wondering if an arraylist list will be able to function with a JOptionPane window. I am trying to branch out from just using the command console in windows so I am trying to understand how to work with JOptionPane.
for example psudocode :
import javax.swing.*;
import java.util.*;
import java.io.*;
public class try1
{
private static JPanel panel = new JPanel();
private static try2 testing = new try2 ();
public static Integer testnum;
public static void main (String[] args)
{
testnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter The Amount Of Test To Be Calculated Below "));
tryMe ();
}
public static void tryMe ()
{
int userInput = 0;
Object[] options1 = { " ENTER " , " GET AVERAGE " };
panel.add(new JLabel(" PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE "));
JTextField textField = new JTextField(10);
panel.add(textField);
if (userInput == JOptionPane.YES_OPTION)
{
for ( int count = 1; count <= testnum; count++)
{
userInput = JOptionPane.showOptionDialog(null, panel, " TEST AVERAGE PROGRAM " ,JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null, options1, null);
try2 testing = new try2 (userInput); // sending this to my class.
}
}
if (userInput == JOptionPane.NO_OPTION)
{
testing.setAvg ();
JOptionPane.showMessageDialog(null,"You average is" + (testing.getAvg()));
}
}
}
class try2
{
public static ArrayList<Integer>userInput=new ArrayList<Integer>();
public static double avg;
public try2()
{
}
public try2(int i)
{
userInput.add(i);
}
public static void setAvg ()
{
try
{
int sum = 0;
for ( int x = 0 ; x < userInput.size(); x++)
{
sum += userInput.size() ;
}
avg = sum / userInput.size();
if ( avg < 0 || avg > 100)
{
IllegalArgumentException ex;
}
}
catch ( IllegalArgumentException ex)
{
}
}
public static double getAvg ()
{
return avg;
}
}
I started with this example in order to see how this works, can anyone tell me what I am doing wrong. So this is were I am stuck at the Jpanel comes up, so it is going through my for statement. However, the jpanel does not clear. How would I make the Jpanel clear out so another input can be put in?
"I am trying to branch out from just using the command console in windows so I am trying to understand how to work with JOptionPane."
So it looks like JOptionPane
is your first encounter with any GUI. What I would recommend is to try the simplest of tasks. Some notes first
();
at the end of your ArrauList
declaration, is that your loop is off.
example.size()
for the iterations, which is initially 0. The loop will work, but it will be an infinite loop within an explicit conditional if/break
inside the loop.count >
but rather count <
and use a hard coded number for now.After following the points above, try something simple like
Tterating 10 times showing the JOptionPane
and adding the input number to the list
for (int count = 0; count < 10; count++) {
Integer num = ...
example.add(num);
}
Then after the loop is done, just do something like adding all the numbers up from the ArrayList
and print it out.
Just do a bunch of simple things like this to get the hang of it. Also have a look at How to Make Dialogs for other JOptionPane
dialogs besides the inputDialog
UPDATE with code
import javax.swing.*;
import java.util.*;
import java.io.*;
public class try1 {
private static JPanel panel = new JPanel();
private static try2 testing = new try2();
public static Integer testnum;
public static void main(String[] args) {
testnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter The Amount Of Test To Be Calculated Below "));
tryMe();
}
public static void tryMe() {
int userInput = 0;
if (userInput == JOptionPane.YES_OPTION) {
for (int count = 1; count <= testnum; count++) {
String userInputString = JOptionPane.showInputDialog(null, " PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE ");
int value = Integer.parseInt(userInputString);
testing.addInput(value); // sending this to my class.
}
JOptionPane.showMessageDialog(null, String.valueOf(testing.getAvg()));
}
if (userInput == JOptionPane.NO_OPTION) {
System.exit(0);
}
}
}
class try2 {
public static ArrayList<Integer> userInput = new ArrayList<Integer>();
public static double avg;
public try2() {
}
public void addInput(int value) {
userInput.add(value);
}
public try2(int i) {
userInput.add(i);
}
public double getAvg() {
double sum = 0;
for (Integer value : userInput) {
sum += value;
}
return sum / userInput.size();
}
}