0

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. 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? 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.");
    }
}                   
1
  • 2
    you are first setting the text of valueInput to null and then reading it - what did you expect to be the result? Commented Oct 4, 2016 at 19:22

3 Answers 3

5

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:

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can parse only those strings, which represents numbers. The String variable which is pointing to null is definitely not representing the number. 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");
        }

Comments

0

Do //clear outputArea after you are done parsing the input and saving them into variables!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.