-3

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?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Erick
  • 19
  • 1
  • 7
  • There is no clue of how you are using `JOptionPane` with `ArrayList` – Sazzadur Rahaman Feb 16 '14 at 18:04
  • 3
    Can an arraylist work with a JOptionPane? Yes. If you need specifics, please include specifics. – Taylor Feb 16 '14 at 18:05
  • `" I am not sure if Java made a function for the Arraylist class that can work with JOptionPane;"`: No there is no "magic" method. You will have to code this yourself, but fortunately, it's not too hard to do. Why not give it a try? `"or if an Arraylist should be left with a system console program."`: no, of course this is not true. What you need to do is write some code and experiment. – Hovercraft Full Of Eels Feb 16 '14 at 18:06
  • Yes Thank you Great Advice. I edited my previous post to include an example, please take a look at it and tell me if I am heading in the right direction. – Erick Feb 16 '14 at 18:13
  • Start with what are you trying to do? How are you trying to interact with the user? What information are you trying to get from them or show to them? Your code will show a bunch of JOptionPanes in a series to the user. Are you sure you want to do this? Wouldn't you rather show them ***one*** JOptionPane that holds a JPanel that holds a bunch of text fields or holds a JTable? Again, start with *first principles* -- what do you want the user to experience? – Hovercraft Full Of Eels Feb 16 '14 at 18:17

2 Answers2

0

"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();
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Great Advice Thank you soo much!! I will take your example to start – Erick Feb 16 '14 at 19:58
  • I took your example and put it to the test, can you tell me what I am doing wrong. The Jpanel comes up, so it is going through my for statement. However, the jpanel does not clear so the user could put another input? How would I make the Jpanel clear out so another input can be put in? – Erick Feb 17 '14 at 02:45
  • Your code does not compile. I can cant test it. Its missing some things – Paul Samsotha Feb 17 '14 at 02:50
  • What is `pee` that's still missing – Paul Samsotha Feb 17 '14 at 02:58
  • I apologize, I just added my other class to my main. Now I am sure it should compile for you – Erick Feb 17 '14 at 02:59
  • And what exactly are you expecting your program to do? – Paul Samsotha Feb 17 '14 at 03:01
  • @Erick you are using classes totally wrong! Don't create a `new try2` every iteration of the loop. Make a `addInput` methon that adds to the list and just use `testing.addInput(..)` – Paul Samsotha Feb 17 '14 at 03:09
  • Just us a regular JOPtionPane _without_ the JPanel and text feild. After the end of the loop show a `JOptionPane.showMessageDialog(null, String.valueOf(testing.getAvg())` – Paul Samsotha Feb 17 '14 at 03:18
  • @ peeskillet Thank You, for having patience and pointing me in the right direction. I will post up my finished code tomorrow in order for you to see how I went about completing this code. – Erick Feb 17 '14 at 03:46
  • @ peeskillet Well I totally took your example and ran with it. However I hit a snag in m program. It says that I am dividing by zero? – Erick Feb 19 '14 at 04:53
  • `getAvg` _has_ to be called _after_ at least one value is put into the list. As basic arithmetic goes, you _can't_ divide by 0 :) – Paul Samsotha Feb 19 '14 at 04:59
  • yea I apologize I did not put my whole class up, I just posted my entire class up which has my setAvg and getAvg. I keep playing with my setAvg in order to see if I get it working but it still shows that I am dividing by zero. – Erick Feb 19 '14 at 05:11
  • You should ask another question on the site – Paul Samsotha Feb 19 '14 at 05:12
  • I'm going to roll this question back to it original state before you edited it. Just ask another question. – Paul Samsotha Feb 19 '14 at 05:13
  • I have this question posted but no one seems to give me the right answer if you look at my profile you will see it " Try catch is not catching an bad number input ( Also getting a java.lang.arithmatic exception / zero)" or you can click on the link http://stackoverflow.com/questions/21869923/try-catch-is-not-catching-an-bad-number-input-also-getting-a-java-lang-arithma – Erick Feb 19 '14 at 05:17
0

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;
}

}
Erick
  • 19
  • 1
  • 7