gpt4 book ai didi

java - 整数验证问题并显示用户输入了无效数据

转载 作者:行者123 更新时间:2023-12-04 05:45:22 26 4
gpt4 key购买 nike

这是我作为家庭作业的一部分正在处理的 Java 程序。我不想为我完成工作,但我被卡住了。我对 Java 还很陌生,使用的是 NetBeans IDE 7.0.1。

尝试验证用户输入时,我在处理某些异常时遇到了困难。该程序将根据用户输入记录慈善捐赠。但是,我没有成功验证捐赠金额文本字段输入。如果用户输入文本而不是数字,我希望他们被通知他们的输入无效,并且我希望异常处理来处理错误并在用户修复他们的输入后继续。非常感谢任何帮助。

这是我的代码:

package donorgui;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
import java.awt.*;

public class DonorGUI extends JFrame
{

// Components
private JPanel panel;
private JTextArea results;
private JButton entryButton;
private JButton exitButton;
private JButton clearButton;
private JTextField donorField;
private JTextField charityField;
private JTextField pledgeField;

//create variables
String[] donorName = new String[20];
String[] charityName = new String[20];
double[] donationAmt = new double[20];
int i = 0;


// Constants for the window size
private final int WINDOW_WIDTH = 750;
private final int WINDOW_HEIGHT = 510;

//Constructor
public DonorGUI(){

// Set the title.
setTitle("Donor");

// Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Build the panel that contains the other components.
buildPanel();

// Add the panel to the content pane.
add(panel);

// Size and display the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
}

//The buildPanel method creates a panel containing other components.
private void buildPanel(){

// Create labels to display instructions.
JLabel message1 = new JLabel("Name of the Donor:");
JLabel message2 = new JLabel("Name of the Charity:");
JLabel message3 = new JLabel("Amount of the Pledge:");

//instantiate the results area
results = new JTextArea(25,60);

// Create text fields to receive user input
donorField = new JTextField(10);
charityField = new JTextField(10);
pledgeField = new JTextField(10);


//create the user buttons to cause action
entryButton = new JButton("Enter Donation.");
entryButton.addActionListener(new EntryButtonListener());
exitButton = new JButton("EXIT");
exitButton.addActionListener(new ExitButtonListener());
clearButton = new JButton ("Clear Fields");
clearButton.addActionListener(new ClearButtonListener());


// Create a panel.
panel = new JPanel();

//set the LayoutManager
panel.setLayout(new FlowLayout());

// Add the labels, text fields, and button to the panel.
panel.add(message1);
panel.add(donorField);
panel.add(message2);
panel.add(charityField);
panel.add(message3);
panel.add(pledgeField);
panel.add(results);
panel.add(entryButton);
panel.add(exitButton);
panel.add(clearButton);

}
private class EntryButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

donorName[i] = donorField.getText();
charityName[i] = charityField.getText();
donationAmt[i] = Double.parseDouble(pledgeField.getText());
results.append(donorName[i]+" "+charityName[i]+" "+donationAmt[i]+"\n ");
i++;
}
}
public boolean donationAmt(String amount) {
int i = 0;
try {
i = Integer.parseInt (amount);
return true;
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid Input. Please enter a dollar amount");
return false;
}


}
private class ClearButtonListener implements ActionListener {
@Override
public void actionPerformed (ActionEvent e) {
donorField.setText("");
charityField.setText("");
pledgeField.setText("");
}
}
private class ExitButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

/* Application method */
public static void main(String[] args){

DonorGUI rpc = new DonorGUI();
}

}

这是我修改后的代码。经过一些用户输入测试后,当我使用 100.00、40.00 等而不是整个美元金额(例如 100、40 等)时,出现异常。
有什么想法吗?

package donorgui;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
import java.awt.*;

public class DonorGUI extends JFrame
{
// Components
private JPanel panel;
private JTextArea results;
private JButton entryButton;
private JButton exitButton;
private JButton clearButton;
private JTextField donorField;
private JTextField charityField;
private JTextField pledgeField;

//create variables
String[] donorName = new String[20];
String[] charityName = new String[20];
double[] donationAmt = new double[20];
int i = 0;


// Constants for the window size
private final int WINDOW_WIDTH = 750;
private final int WINDOW_HEIGHT = 550;

//Constructor
public DonorGUI(){

// Set the title.
setTitle("Donor");

// Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Build the panel that contains the other components.
buildPanel();

// Add the panel to the content pane.
add(panel);

// Size and display the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
}

//The buildPanel method creates a panel containing other components.
private void buildPanel(){

// Create labels to display instructions.
JLabel message1 = new JLabel("Name of the Donor:");
JLabel message2 = new JLabel("Name of the Charity:");
JLabel message3 = new JLabel("Amount of the Pledge:");

//instantiate the results area
results = new JTextArea(25,60);


// Create text fields to receive user input
donorField = new JTextField(10);
charityField = new JTextField(10);
pledgeField = new JTextField(10);


//create the user buttons to cause action
entryButton = new JButton("Enter Donation.");
entryButton.addActionListener(new EntryButtonListener());
exitButton = new JButton("EXIT");
exitButton.addActionListener(new ExitButtonListener());
clearButton = new JButton ("Clear Fields");
clearButton.addActionListener(new ClearButtonListener());


// Create a panel.
panel = new JPanel();

//set the LayoutManager
panel.setLayout(new FlowLayout());

// Add the labels, text fields, and button to the panel.
panel.add(message1);
panel.add(donorField);
panel.add(message2);
panel.add(charityField);
panel.add(message3);
panel.add(pledgeField);
panel.add(results);
panel.add(entryButton);
panel.add(exitButton);
panel.add(clearButton);

}
private class EntryButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

donorName[i] = donorField.getText();
charityName[i] = charityField.getText();
if (donationAmt(pledgeField.getText())) {
donationAmt[i] = Double.parseDouble(pledgeField.getText());
}
results.append(donorName[i]+" "+charityName[i]+" "+donationAmt[i]+"\n ");
i++;
}
}

public boolean donationAmt(String amount) {
if(amount==null || amount=="" || amount.length()<1){ //checking for empty field
JOptionPane.showMessageDialog(null, "Please enter a dollar amount");
return false;
}

for(int i = 0; i < amount.length(); i++){ //verifying dollar amount entered as number
if (!Character.isDigit(amount.charAt(i))){
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a dollar amount");
return false;
}
}
return true;

}



private class ClearButtonListener implements ActionListener {
@Override
public void actionPerformed (ActionEvent e) {
donorField.setText("");
charityField.setText("");
pledgeField.setText("");
}
}
private class ExitButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

/* Application method */
public static void main(String[] args){

DonorGUI rpc = new DonorGUI();
}

}

最佳答案

您可以使用 InputVerifier , 讨论 here .

关于java - 整数验证问题并显示用户输入了无效数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10749906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com