[英]Converting String input to Integer from JTextField with Integer.parseInt but still receiving error message
Code: 码:
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);
}
}
}
If you're getting a NumberFormatException then java can't parse whatever it is as a number. 如果您收到NumberFormatException,则Java无法将其解析为数字。
Other things to check for would be empty strings "" or null strings (in which case it may throw a null pointer exception). 要检查的其他内容是空字符串“”或空字符串(在这种情况下,它可能会引发空指针异常)。
Maybe also check if there is spurious whitespace - get rid of it with a trim eg 也许还检查是否有虚假的空格-修剪掉它,例如
//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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.