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?