0

I am trying to add the user input from the text field into an array when I click the Add Button. The Display Button displays the whole array. I already initialized the array (array = new String[10]) else where in the program. I initialized the counter to 0 as a private variable. If the user enters more than 10 strings, the input should not be added. Any advice?

    // if add button is clicked
    if (e.getSource() == btnAdd){

        /*input = textInput.getText();
        output = textOutput.getText() + input + "\n";
        textOutput.setText(output);*/

        // get user input from text field
        input = textInput.getText();  

        // clear text input field
        textInput.setText("");

        /*do {

            array[counter] = input;
            counter++;

        } while (counter < 11);

        if (counter > 10){

            picBottom = new TextPicture(new String("No Space Available"), new Font("TimesRoman", Font.ITALIC, 30), new Color(0));
            picBottom.setC(Color.RED);
        }*/

        // add user input to array if counter is within array length
        if (counter <= 10){

            array[counter] = input;
            counter++;
        }

        // display no more space available if counter is outside of array length
        else {

            picBottom = new TextPicture(new String("No Space Available"), new Font("TimesRoman", Font.ITALIC, 30), new Color(0));
            picBottom.setC(Color.RED);
        }
    }

    // if display button is clicked
    else if (e.getSource() == btnDisplay){

        // go through array & display each string
        for (int i = 0; i <= counter; i++){

            output += array[i];
            textOutput.setText(output);
        }
    }
5
  • What about array = textfield.getText().toCharArray();? Commented Mar 28, 2020 at 15:27
  • The array is a String array Commented Mar 28, 2020 at 16:06
  • 1
    And what is the problem? Posted code looks reasonable. Did you add any debug code to your if statement to see if the code is executed? Commented Mar 28, 2020 at 16:08
  • When I enter over 10 inputs, errors pop up on the console screen. I believe the error is within the if else statements when the add button is clicked. The display button displays "null" before and after the array if there are less than 10 inputs. Commented Mar 28, 2020 at 17:45
  • counter starts counting by 0. So you have change: if (counter <= 10) to if (counter < 10) so counter can only be 0, 1, ....8, 9 like your array is defined. Commented Apr 2, 2020 at 15:59

1 Answer 1

1

Here you have a minimum example:

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 300, 300);

JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 150));

JTextField txtField = new JTextField("");
txtField.setPreferredSize(new Dimension(200, 30));

JButton button = new JButton("Push me");
button.addActionListener((ActionEvent arg0) -> {
    if(counter < 10){
        array[counter] = txtField.getText();
        counter++;
        txtField.setText("");
        txtField.requestFocus();
    }
    else{
        System.out.println("No Space Available");
    }


    System.out.println(Arrays.toString(array));
});
panel.add(txtField);
panel.add(button);

frame.add(panel);
frame.setVisible(true);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help, that makes more sense now

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.