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

时间:2018-06-04 04:17:07

标签: java swing integer jtextfield

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

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

1 个答案:

答案 0 :(得分:1)

如果你得到一个NumberFormatException,那么java就无法解析它的数字。

要检查的其他事项是空字符串""或null字符串(在这种情况下,它可能抛出空指针异常)。

也许还要检查是否存在虚假的空白 - 例如用一个修剪去掉它。

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