"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
- The thing about your code, besides the fact that your missing
();
at the end of yourArrauList
declaration, is that your loop is off.- You are trying to use
example.size()
for the iterations, which is initially 0. The loop will work, but it will be an infinite loop within an explicit conditionalif/break
inside the loop. - You should't be checking if
count >
but rathercount <
and use a hard coded number for now.
- You are trying to use
After following the points above, try something simple like
Tterating 10 times showing the
JOptionPane
and adding the input number to the listfor (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();
}
}