简体   繁体   English

在Java中将整数添加到数组

[英]Adding Integers to an Array in Java

I have to create a program that accepts user input of numbers and then adds them to an ArrayList and then manipulates the data in several ways. 我必须创建一个程序来接受用户输入的数字,然后将它们添加到ArrayList中,然后以几种方式处理数据。 The numbers must greater than or equal to 0. I am having an issue with adding the user input to the ArrayList, my try and catch statements stop the program from crashing but I can't add anything to the ArrayList, can someone tell me what I'm doing wrong in my adding process? 数字必须大于或等于0。我在将用户输入添加到ArrayList时遇到问题,我的try和catch语句阻止程序崩溃,但是我无法在ArrayList中添加任何内容,有人可以告诉我什么吗?添加过程中我做错了吗? Here's my code: 这是我的代码:

import java.util.ArrayList;
import java.util.Collections;

public class SumElements extends javax.swing.JFrame {
ArrayList <Integer> values = new ArrayList();

... ...

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    try
    {

        //clear outputArea
        outputArea.setText(null);
        valueInput.setText(null);
        outputLabel.setText(null);

        //declare variables
        int value = Integer.parseInt(valueInput.getText());

        //validate input
        if (value >= 0){
            //add item to array
            values.add(value);

            //display values
            Collections.sort(values);
            for (int i = 0; i < values.size(); i++)
            {
                outputArea.setText(outputArea.getText() + values.get(i) + "\n");
            }
        }
    }
    //set default
    catch (NumberFormatException a)
    {
     outputLabel.setText("Please input a valid number.");
    }
}                   

This is because you set the text of valueInput to null (with valueInput.setText(null) ) before calling Integer.parseInt(valueInput.getText()) which will throw a NumberFormatException of the following type: 这是因为你设置的文本valueInputnull (用valueInput.setText(null)之前调用) Integer.parseInt(valueInput.getText())这将抛出一个NumberFormatException的以下类型:

Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)

So simply remove the line valueInput.setText(null); 因此,只需删除行valueInput.setText(null);

You can parse only those strings, which represents numbers. 您只能解析那些代表数字的字符串。 The String variable which is pointing to null is definitely not representing the number. 指向null的String变量绝对不代表数字。 What I would suggest is to modify the code in this way: 我建议以这种方式修改代码:

    //declare variables
    int value = Integer.parseInt(valueInput.getText());

    //clear outputArea
    valueInput.setText("");
    outputLabel.setText("");

    //validate input
    if (value >= 0){
        //add item to array
        values.add(value);

        //display values
        Collections.sort(values);
        for (int i = 0; i < values.size(); i++) {
            outputArea.setText(outputArea.getText() + values.get(i) + "\n");
        }

在完成对输入的解析并将其保存为变量之后,请// //清除outputArea!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM