how to store strings in a while loop in java

2.1k views Asked by At

I am trying to store the data that's created in the while loop in the list variable so that I can use it later with the JOptionPane.INFORMATION_MESSAGE. I tried using an array to store the data but it would not work. Please let me know what I am missing.

package loops;

import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Statistics
{

    public static void main(String[] args)
    {
        int observations = 1,
            num = 0,
            sum = 0,
            max = Integer.MIN_VALUE,
            min = Integer.MAX_VALUE;
        double mean = 0.0;

        String userEntry = "",
               result,
               list = " ",
               seperator = "\n***********\nYou entered the following observations: ";

        DecimalFormat twoDigits = new DecimalFormat ("0.00");

        userEntry = JOptionPane.showInputDialog ("Enter observation # " + observations + 
                 " (or \"end\" to quit) ");
        //num = Integer.parseInt(userEntry);

        String[] list2 = new String[num];

        while(!userEntry.equalsIgnoreCase("end"))
        {       
            num = Integer.parseInt(userEntry);          
            observations ++;
            userEntry = JOptionPane.showInputDialog ("Enter observation #" + observations + 
                         " (or \"end\" to quit) ");
            //num = Integer.parseInt(userEntry);
            //num = Integer.parseInt(userEntry);
            //list = "\n" + num;
            //list = list.toString();
            sum += num;     

            if(num > max)
            {
                max = num;
            }

            if(num < min)
            {
                min = num;
            }
            //Integer.toString(num);
            //ArrayList<String> list = new ArrayList<String>();
            //list.add(num);

            //list = "\n" + Integer.toString(num);
            //String[] list2 = new String[num];
            //for(String list1 : list2)
            //{
            list  = "\n" + num;
            //}

        }
        observations --;
        mean = sum / (double)observations;
        //twoDigits.format(mean);

        if(observations == 0)
        {
            result = "no observations selected";
        }

        else
        {
            result = "You entered " + observations + 
                      (observations == 1 ? " observation" : " observations");
            result = result + "\nThe minimum is " + min;
            result = result + "\nThe maximum is " + max;
            result = result + "\nThe sum is " + sum;
            result = result + "\nThe mean is " + twoDigits.format(mean);
            result = result + seperator;
            result = result + list;
        }
        JOptionPane.showMessageDialog(null, result, 
                "Results", JOptionPane.INFORMATION_MESSAGE);


        //observations --;

        System.exit(0);
    }

}
2

There are 2 answers

0
John On

Try using ArrayList instead of []. Example based on your code is shown below.

package loops;

import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Statistics {

    public static void main(String[] args) {
        // method local variables
        int observations = 0;
        int num = 0;
        int sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        double mean = 0.0;
        String userEntry = "";
        String result;
        String list = " "; 
        String seperator = "\n***********\nYou entered the following observations: ";
        DecimalFormat twoDigits = new DecimalFormat("0.00");
        ArrayList<Integer> intList = new ArrayList<Integer>();
        // get user entry from JOptionPane
        userEntry = JOptionPane.showInputDialog("Enter observation # " + observations + " (or \"end\" to quit) ");
        // iterate until user enters end
        while (userEntry.equalsIgnoreCase("end") == false) {
            observations++;
            num = Integer.parseInt(userEntry);
            userEntry = JOptionPane.showInputDialog("Enter observation #" + observations + " (or \"end\" to quit) ");
            sum = sum + num;
            // change number to max if > max
            if (num > max) {
                max = num;
            }
            // change number to min if < min
            if (num < min) {
                min = num;
            }
            // add the number to the string and to the list
            list = list + "\n" + num;
            intList.add(num);
        }
        echoList(intList);
        mean = sum / (double) observations;
        if (observations == 0) {
            result = "no observations selected";
        }
        else {
            result = "You entered " + observations + (observations == 1 ? " observation" : " observations");
            result = result + "\nThe minimum is " + min;
            result = result + "\nThe maximum is " + max;
            result = result + "\nThe sum is " + sum;
            result = result + "\nThe mean is " + twoDigits.format(mean);
            result = result + seperator;
            result = result + list;
        }
        // show results and exit
        JOptionPane.showMessageDialog(null, result, "Results", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }

    private static void echoList(ArrayList<Integer> intList) {
        System.out.println("------------------------------");
        System.out.println("Got " + intList.size() + " integers.");
        for(Integer i : intList) {
            System.out.println("\t" + i);
        }
        System.out.println("------------------------------");
    }

}
0
Srini On

JOptionPane.showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise.

Therefore, if the user hits cancel, your while condition (userEntry.equalsIgnoreCase("end") == false) will throw a NullPointerException.

Therefore, it's safer to change the while condition to (userEntry != null).

Now, if you want to store all the user entries, use a list of integers, say List<Integer> userEntries

List<Integer> userEntries = new ArrayList<>();
while (userEntry.equalsIgnoreCase("end") == false) 
{
    observations++;
    num = Integer.parseInt(userEntry);
    userEntries.add(num);
    ...
}