2016-04-14 11 views
0

私のWithDraw/Depositとその下に私のonSubmitの機能があります。あなたはInteger.parseInt()を使用してイムを見ることができます...しかし、私はそれを使用する場合、それは私にNumberFormatExceptionを与える:ここでNumberFormat実行

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を見てください...使用してイム私のコードです。 Integer.parseInt(); .. int型に文字列を変換しonSubmitがあることを内部:

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

まあ、withdrawLabelでは、withdrawInputフィールドの代わりに 'Integer.parseInt'を実行します。 'String withDrawString = withdawLabel.getText();'は 'String withDrawString = withdaw.getText();'でなければなりません。このエラーは、ラベルテキストをフォーマットしようとしていることさえ示しています( 'NumberFormatException:入力文字列:"入金/出金額: ")。 –

+0

Omg @ KevinCruijssen。 biengばかげて申し訳ありません –

答えて

0

問題は、ラベルのではなく、フィールドのテキストを解析しようということです。ここ

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

あなたが知る限り、数ない"Amount of deposit/withdrawal:"あるwithDrawStringにおけるwithdawLabelのテキストを、格納、したがって、intとして解析することができません。代わりに、あなたがしなければならない:サイドノートとして

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


を、withdawLabelのスペルが間違っています。

関連する問題