Question

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?

Was it helpful?

Solution

"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 your ArrauList declaration, is that your loop is off.
    1. 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 conditional if/break inside the loop.
    2. You should't be checking if count > but rather count < and use a hard coded number for now.

After following the points above, try something simple like

  1. 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);
    }
    
  2. 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();
    }
}

OTHER TIPS

This is my test program I created using an array-list and Jopt. I will have to tweek my for statement's counter in my main but that will be a piece of cake. Thank you @peeskillet for your help

// Import Libraries

    import javax.swing.*;
    import java.util.*;



public class Try1

{       
public static ArrayList<Integer>user=new ArrayList<Integer>();
private static JPanel panel = new JPanel();
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 "));
        classes ();
}
public static void classes() 
{
    int userInput = 0;
    if (userInput == JOptionPane.YES_OPTION) 
    {
        user = new ArrayList<Integer>();
        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);
            user.add(value);
            new Try2(user);
        }
    }
        if (userInput == JOptionPane.NO_OPTION) 
        {
            Try2.setAvg();
            JOptionPane.showMessageDialog(null, "You average is" + (Try2.getAvg()));
        }
            classesExtended();          
}
public static void classesExtended()
{
    JFrame frame = new JFrame();
    String[] options = new String[3];
    options[0] = new String ( " GET AVERAGE ");
    options[1] = new String ( " OOPS I FORGOT!! ADD MORE TESTS ");
    options[2] = new String ( " Exit ");
    int result = JOptionPane.showOptionDialog(frame.getContentPane(), " OK WHAT WOULD YOU LIKE TO DO NOW ","Title", 0,JOptionPane.INFORMATION_MESSAGE,null,options,null);

    if ( result == 0 )
    {
        Try2.setAvg ();
        JOptionPane.showMessageDialog(null,"You average is" + (Try2.getAvg()));
    }
    if ( result == 1 )
    {
         classes ();
    }
    if ( result == 2)
    {
        System.exit(0);
    }       
}       
}


class Try2
{
    public static ArrayList<Integer>userInput=new ArrayList<Integer>();
    public static double avg;

public Try2()
{
}
public Try2(ArrayList<Integer> test) 
{
    try
    {   
        for (int x = 0; x <= test.size()-1; x++) 
        {
            if (test.get(x) < 0 || test.get(x) > 100) 
            {   
                throw new IllegalArgumentException();   
            } 
            else
            {
                this.userInput = test;
            }
        }
    }
    catch (IllegalArgumentException ex) 
    {
        JOptionPane.showMessageDialog(null, " NO NEGETIVES ALLOWED ");
    }
}

public static void setAvg ()
{
        int sum = 0;

        for ( int x = 0 ; x < userInput.size(); x++)
        {
            sum += userInput.get(x) ;
        }
        avg = sum / userInput.size();   
}
public static double getAvg ()
{
    return avg;
}

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top