有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NumberFormat执行选项

所以你可以在下面看到我的取款/存款和我的onSubmit功能。你可以看到im使用Integer.parseInt()。。。但当我使用它时,它会给我NumberFormatException

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Amount of deposit/withdrawal:"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at LocalBank.actionPerformed(LocalBank.java:179)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这是我使用的代码。。。看看actionPerformed里面的“撤回”支票。里面有一个onSubmit将字符串转换为intInteger.parseInt();

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.math.*;
import java.security.*;
import java.util.UUID;


public class LocalBank implements ActionListener {

    public Map <String,String> infoUser = new HashMap<String, String>();
    private JFrame frame;
    private JPanel contentPane;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField accNumber;
    private JTextField withdraw;
    private JTextField text3;
    private JButton  avgButton;
    private JLabel average;
    private JComboBox plantNames;
    private JTextField firstname;
    private JTextField lastname;
    private JTextField beggining;
    private JButton submit;
    private JLabel accNumLabel;
    private JLabel withdawLabel;
    private JLabel firstnameLabel;
    private JLabel lastnameLabel;
    private JLabel begginingLabel;

    public LocalBank() {
        // TODO Auto-generated constructor stub
        frame = new JFrame("Semiseter");


        contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder
                (20, 20, 20, 20));


        label1 = new JLabel("Enter An Integer");
        label1.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        contentPane.add(label1);


        String[] names = {"Deposit", "Withdrawal", "Check Balance","Add Account","Remove Account"};
        plantNames = new JComboBox(names);
        plantNames.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
        plantNames.setSelectedIndex(0);
        plantNames.addActionListener(this);
        contentPane.add(plantNames);

        accNumLabel = new JLabel("Account Number");
        contentPane.add(accNumLabel);
        accNumber = new JTextField();
        contentPane.add(accNumber);

        withdawLabel = new JLabel("Amount of deposit/withdrawal:");
        contentPane.add(withdawLabel);

        withdraw = new JTextField("");
        contentPane.add(withdraw);

        firstnameLabel = new JLabel("First Name:");
        contentPane.add(firstnameLabel);

        firstname = new JTextField();
        contentPane.add(firstname);

        lastnameLabel = new JLabel("Last Name:");
        contentPane.add(lastnameLabel);

        lastname = new JTextField("");
        contentPane.add(lastname);

        begginingLabel = new JLabel("Beginning Balance:");
        contentPane.add(begginingLabel);

        beggining = new JTextField("");
        contentPane.add(beggining);

        submit = new JButton("Process Transaction");
        submit.addActionListener(this);
        submit.setActionCommand("Submit");
        contentPane.add(submit);

        frame.setContentPane(contentPane);

         /* Size and then display the frame. */
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);



    }



    private static void runGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        LocalBank greeting = new LocalBank();
    }
    public static void main(String[] args) {
         /* Methods that create and show a GUI should be
         run from an event-dispatching thread */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runGUI();
            }
        });


    }



    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub

        //ArrayList<String> infoUser = new ArrayList<String>();

        JComboBox comboBox = plantNames;
        String plantName = (String)comboBox.getSelectedItem();
        String eventString = event.getActionCommand();
        if ("Deposit".equals(plantName)) {
            accNumLabel.setVisible(true);
            accNumber.setVisible(true);

            withdawLabel.setVisible(true);
            withdraw.setVisible(true);

            firstnameLabel.setVisible(false);
            firstname.setVisible(false);

            lastnameLabel.setVisible(false);
            lastname.setVisible(false);

            begginingLabel.setVisible(false);
            beggining.setVisible(false);

            if(eventString.equals("Submit")) {
                String balanceString = infoUser.get("Beggining");
                int balance = Integer.parseInt(balanceString);
                String withDrawString = withdawLabel.getText();
                int deposit = Integer.parseInt(withDrawString);
                int currentBalance = balance + deposit;
                infoUser.put("Beggining", String.valueOf(currentBalance));
                System.out.println("CURRENT BALANCE " + infoUser.get("Beggining"));
            }

        }
            else if ("Withdrawal".equals(plantName)) {
            accNumLabel.setVisible(true);
            accNumber.setVisible(true);

            withdawLabel.setVisible(true);
            withdraw.setVisible(true);

            firstnameLabel.setVisible(false);
            firstname.setVisible(false);

            lastnameLabel.setVisible(false);
            lastname.setVisible(false);

            begginingLabel.setVisible(false);
            beggining.setVisible(false);

            if(eventString.equals("Submit")) {
                String balanceString = infoUser.get("Beggining");
                int balance = Integer.parseInt(balanceString);
                String withDrawString = withdawLabel.getText();
                int withdraw = Integer.parseInt(withDrawString);
                int currentBalance = balance - withdraw;
                infoUser.put("Beggining", String.valueOf(currentBalance));
                System.out.println("CURRENT BALANCE " + infoUser.get("Beggining"));
            }

        }
            else if ("Check Balance".equals(plantName)) {

            accNumLabel.setVisible(true);
            accNumber.setVisible(true);

            firstnameLabel.setVisible(false);
            firstname.setVisible(false);

            lastnameLabel.setVisible(false);
            lastname.setVisible(false);

            begginingLabel.setVisible(false);
            beggining.setVisible(false);

            withdawLabel.setVisible(false);
            withdraw.setVisible(false);

            if(eventString.equals("Submit")){
                System.out.println("*****ACCOUNT INFO ****");
                System.out.println("FIRSTNAME : " + infoUser.get("Firstname"));
                System.out.println("LASTNAME : " + infoUser.get("Lastname"));
                System.out.println("Price : " + infoUser.get("Beggining"));
                System.out.println("ACCOUNT NUMBER : " + infoUser.get("AccNum"));


            }
        }
            else if ("Add Account".equals(plantName)) {
            accNumLabel.setVisible(false);
            accNumber.setVisible(false);

            withdawLabel.setVisible(false);
            withdraw.setVisible(false);

            firstnameLabel.setVisible(true);
            firstname.setVisible(true);

            lastnameLabel.setVisible(true);
            lastname.setVisible(true);

            begginingLabel.setVisible(true);
            beggining.setVisible(true);

            if(eventString.equals("Submit")){
                String AccNumberGenerated = UUID.randomUUID().toString();
                System.out.println("Account Added with FirstName : " + firstname.getText()+ " Lastname " + lastname.getText() + " And Beggining Balance : " + beggining.getText() + "And Finally Account Number of " + AccNumberGenerated);
                infoUser.put("Firstname" , firstname.getText());
                infoUser.put("Lastname" , lastname.getText());
                infoUser.put("Beggining" , beggining.getText());
                infoUser.put("AccNum" , AccNumberGenerated);


            }
        }
        else if ("Remove Account".equals(plantName)) {
            accNumLabel.setVisible(true);
            accNumber.setVisible(true);

            withdawLabel.setVisible(false);
            withdraw.setVisible(false);

            firstnameLabel.setVisible(false);
            firstname.setVisible(false);

            lastnameLabel.setVisible(false);
            lastname.setVisible(false);

            begginingLabel.setVisible(false);
            beggining.setVisible(false);

            if(eventString.equals("Submit")){
                System.out.println("ACCOUNT DELETED");
                frame.setVisible(false);
                frame.dispose();
                System.exit(0);
            }
        }
        else{
            accNumLabel.setVisible(false);
            accNumber.setVisible(false);

            withdawLabel.setVisible(false);
            withdraw.setVisible(false);

            firstnameLabel.setVisible(false);
            firstname.setVisible(false);

            lastnameLabel.setVisible(false);
            lastname.setVisible(false);

            begginingLabel.setVisible(false);
            beggining.setVisible(false);
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    问题是,您试图解析标签的文本,而不是字段的文本

    这里:

    String withDrawString = withdawLabel.getText();
    int withdraw = Integer.parseInt(withDrawString);
    

    withdawLabel的文本存储在withDrawString中,即"Amount of deposit/withdrawal:",它不是一个数字,因此不能被解析为int。相反,你必须做到:

    String withDrawString = withdraw.getText();
    int withdraw = Integer.parseInt(withDrawString);
    


    作为旁注,withdawLabel的拼写是错误的