java - 使用 Integer.parseInt 将字符串输入从 JTextField 转换为整数,但仍然收到错误消息

标签 java swing integer jtextfield

  1. textSeventh 是一个 JTextField
  2. 我正在使用带有操作监听器的提交按钮

代码:

String amountInput = textSeventh.getText();
System.out.println(amountInput);

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UI2 implements ActionListener {

    public static void main(String[] args) {
        // Dont use the static context. Make an instance variable and call your method.
        UI ui = new UI();
        ui.makeUI();
    }

    static String questionFirst = "What is your first name?";
    static String questionSecond = "What is your last name?";
    static String questionThird = "What month were you born? Enter one     number.";
    static String questionFourth = "What year were you born?";
    static String questionFifth = "What day were you born?";
    static String questionSixth = "What is your bank account number";
    static String questionSeventh = "How much is in your bank account?   Include decimals.";

    public void makeUI() {
        makeBox(questionFirst, questionSecond, questionThird, questionFourth, questionFifth, questionSixth, questionSeventh);
    }

    static JFrame frame = new JFrame("FortuneTeller");
    static JPanel panel = new JPanel(new GridBagLayout());

    static JLabel labelFirst = new JLabel();
    static JTextField textFirst = new JTextField(50);

    static JLabel labelSecond = new JLabel();
    static JTextField textSecond = new JTextField(50);

    static JLabel labelThird = new JLabel();
    static JTextField textThird = new JTextField(50);

    static JLabel labelFourth = new JLabel();
    static JTextField textFourth = new JTextField(50);

    static JLabel labelFifth = new JLabel();
    static JTextField textFifth = new JTextField(50);

    static JLabel labelSixth = new JLabel();
    static JTextField textSixth = new JTextField(50);

    static JLabel labelSeventh = new JLabel();
    static JTextField textSeventh = new JTextField(50);

    static JButton submitButton = new JButton("Submit");

    static String firstName;
    static String lastName;
    static String month;
    static String year;
    static String day;
    static String bankNum;
    static String amount;

    static char favoriteLetter;
    static String both;
    static String reverse;
    static String favoritePalindrome;
    static String favoriteColor; // red or blue
    static String favoriteAnimal; // cat or dog
    static String favoriteCar; // F150 or Minivan
    static String favoriteNum;
    static int intDollars;
    static String math;

    public void makeBox(String questionFirst, String questionSecond, String questionThird, String questionFourth, String questionFifth, String questionSixth,
            String questionSeventh) {
        frame.add(panel);
        frame.setVisible(true);
        frame.setSize(700, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setLayout(new GridLayout(8, 1));
        // Now I am only trying to get input from the first box
        panel.add(labelFirst);
        labelFirst.setText(questionFirst);
        panel.add(textFirst);

        // This is the advice from my school's computer science teacher when we thought
        // that
        // the program was printing the initial value inside the textfield, nothing.
        // Enter text
        // Wait for submit button
        // Then getText();
        // I was still unable to get it to work

        panel.add(labelSecond);
        labelSecond.setText(questionSecond);
        panel.add(textSecond);

        // get text will be empty here. You should be calling this after the user enters text and clicks submit.
        lastName = textSecond.getText();

        panel.add(labelThird);
        labelThird.setText(questionThird);
        panel.add(textThird);

        // get text will be empty here. You should be calling this after the user enters text and clicks submit.
        month = textThird.getText();

        panel.add(labelFourth);
        labelFourth.setText(questionFourth);
        panel.add(textFourth);

        // get text will be empty here. You should be calling this after the user enters text and clicks submit.
        year = textFourth.getText();

        panel.add(labelFifth);
        labelFifth.setText(questionFifth);
        panel.add(textFifth);

        // get text will be empty here. You should be calling this after the user enters text and clicks submit.
        day = textFifth.getText();

        panel.add(labelSixth);
        labelSixth.setText(questionSixth);
        panel.add(textSixth);
        // get text will be empty here. You should be calling this after the user enters text and clicks submit.
        bankNum = textSixth.getText();

        panel.add(labelSeventh);
        labelSeventh.setText(questionSeventh);
        panel.add(textSeventh);
        // get text will be empty here. You should be calling this after the user enters text and clicks submit.
        amount = textSeventh.getText();

        // need to add an actionListener to the button
        submitButton.addActionListener(this);

        panel.add(submitButton);
        frame.pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub

        // You need to get the source of the button. (JButton) e.getSource();
        JButton buttonPressed = (JButton) e.getSource();
        if (buttonPressed == submitButton) {
            String firstNameInput = textFirst.getText();
            System.out.println(firstNameInput);
        }
    }
}

最佳答案

如果您收到 NumberFormatException,那么 java 无法将其解析为数字。

其他要检查的内容是空字符串“”或空字符串(在这种情况下可能会引发空指针异常)。

也许还可以检查是否存在虚假空格 - 例如通过修剪将其删除

//get favorite number
String amountInput = textSeventh.getText();

if (amountInput == null) {
    System.err.println("amountInput was null, no point continuing");
    return;
}

// make it a bit more obvious if there's whitespace
System.out.println("amountInput = +++" + amountInput + "+++");

amountInput = amountInput.trim();

if (amountInput.equalsIgnoreCase("")) {
    System.err.println("There's nothing there!!");
}

int dollars = -1337;
try {
    dollars = Integer.parseInt(amountInput);
} catch (Exception e) {
    System.err.println("Error when parsing value.\n" + e);
    // optional
    // e.printStackTrace();
}
System.out.println(dollars);

关于java - 使用 Integer.parseInt 将字符串输入从 JTextField 转换为整数,但仍然收到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50673334/

相关文章:

java - 在框架上渲染 Canvas 和面板以进行动画时出现问题

java - 如何在扩展 JPanel 的类中创建 bufferstrategy

python - 初学者Python程序

c - 在 C 中加载,整数和字符

java - Junit 测试中 SpringCloud Azure 适配器出现 NullPointerException

java - @Valid 在 dropwizard API 资源中有效

java - 从上次测试中恢复测试用例

java - Android BottomNavigationBar 使用 ConstraintLayout 和 ScrollView

java - 将多个组件添加到 Jframe.getcontentpane()

C++如何将我认为是字符串的转换为int?尝试使用 stoi,但会使用任何有效的方法