如何在java中的while循环中存储字符串

时间:2015-06-10 05:55:15

标签: java while-loop joptionpane

我正在尝试将在while循环中创建的数据存储在list变量中,以便稍后我可以将它与JOptionPane.INFORMATION_MESSAGE一起使用。 我尝试使用数组来存储数据,但它不起作用。 请让我知道我错过了什么。

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 个答案:

答案 0 :(得分:0)

尝试使用ArrayList而不是[]。基于您的代码的示例如下所示。

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("------------------------------");
    }

}

答案 1 :(得分:0)

JOptionPane.showInputDialog()将返回用户点击 ok 时输入的string,否则返回null

因此,如果用户点击取消,则您的while条件(userEntry.equalsIgnoreCase("end") == false)会抛出NullPointerException

因此,将while条件更改为(userEntry != null)会更安全。

现在,如果要存储所有用户条目,请使用整数列表,例如List<Integer> userEntries

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