线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:输入字符串:“”同时使用awt在计算器中添加两个数字

问题描述 投票:0回答:1

我不知道为什么会出现这个错误我有下面的java代码

我试图制作一个计算器,每当我尝试将两个数字相加时,它都会显示此错误 我还将在下面给出完整的错误 线程“AWT-EventQueue-0”中的异常 java.lang.NumberFormatException:对于输入字符串:“” 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:592) 在 java.lang.Integer.parseInt(Integer.java:615) 在 calculator.actionPerformed(calculator.java:282) 在 java.awt.Button.processActionEvent(Button.java:409) 在 java.awt.Button.processEvent(Button.java:377) 在 java.awt.Component.dispatchEventImpl(Component.java:4889) 在 java.awt.Component.dispatchEvent(Component.java:4711) 在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) 在 java.awt.EventQueue.access$500(EventQueue.java:97) 在 java.awt.EventQueue$3.run(EventQueue.java:709) 在 java.awt.EventQueue$3.run(EventQueue.java:703) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84) 在 java.awt.EventQueue$4.run(EventQueue.java:733) 在 java.awt.EventQueue$4.run(EventQueue.java:731) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:730) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

对不起,因为这是一个大代码

import java.awt.*;
import java.awt.event.*;
class calculator extends WindowAdapter implements ActionListener
{
    //Variables
    int point=0,res=0,ractive=0,n1active=0,n2active=0,opactive=0,inc=0,lng=0;
    String val="",num1="",num2="";
    
    //Frame object
    Frame f=new Frame();
    
    //Text Box objects
    TextField t1=new TextField(255);
    
    //function Buttons 
    Button bce=new Button("CE");
    Button bc=new Button("C");
    Button bba=new Button("Del");
    Button b1x=new Button("1/x");
    Button bx2=new Button("x^2");
    Button b2x=new Button("sqrt x");
    
    //Number Buttons
    Button b0=new Button("0");
    Button b1=new Button("1");
    Button b2=new Button("2");
    Button b3=new Button("3");
    Button b4=new Button("4");
    Button b5=new Button("5");
    Button b6=new Button("6");
    Button b7=new Button("7");
    Button b8=new Button("8");
    Button b9=new Button("9");
    Button bp=new Button(".");
    Button bnp=new Button("+/-");
    
    //Operator Buttons
    Button badd=new Button("+");
    Button bsub=new Button("-");
    Button bmul=new Button("X");
    Button bdiv=new Button("/");
    Button bres=new Button("=");
    Button bper=new Button("%");
    
    void Cal_UI()
    {
        //Text Box edits
        t1.setBounds(25,45,255,44);
        
        //Button Layer 1
        bper.setBounds(25,110,62,40);
            bper.addActionListener(this);
        bce.setBounds(87,110,62,40);
            bce.addActionListener(this);
        bc.setBounds(149,110,62,40);
            bc.addActionListener(this);
        bba.setBounds(211,110,62,40);
            bba.addActionListener(this);
        
        //Button Layer 2
        b1x.setBounds(25,150,62,40);
            b1x.addActionListener(this);
        bx2.setBounds(87,150,62,40);
            bx2.addActionListener(this);
        b2x.setBounds(149,150,62,40);
            b2x.addActionListener(this);
        bdiv.setBounds(211,150,62,40);
            bdiv.addActionListener(this);
        
        //Button Layer 3
        b7.setBounds(25,190,62,40);
            b7.addActionListener(this);
        b8.setBounds(87,190,62,40);
            b8.addActionListener(this);
        b9.setBounds(149,190,62,40);
            b9.addActionListener(this);
        bmul.setBounds(211,190,62,40);
            bmul.addActionListener(this);
        
        //Button Layer 4
        b4.setBounds(25,230,62,40);
            b4.addActionListener(this);
        b5.setBounds(87,230,62,40);
            b5.addActionListener(this);
        b6.setBounds(149,230,62,40);
            b6.addActionListener(this);
        bsub.setBounds(211,230,62,40);
            bsub.addActionListener(this);
        
        //Button Layer 5
        b1.setBounds(25,270,62,40);
            b1.addActionListener(this);
        b2.setBounds(87,270,62,40);
            b2.addActionListener(this);
        b3.setBounds(149,270,62,40);
            b3.addActionListener(this);
        badd.setBounds(211,270,62,40);
            badd.addActionListener(this);
        
        //Button Layer 6
        bnp.setBounds(25,310,62,40);
            bnp.addActionListener(this);
        b0.setBounds(87,310,62,40);
            b0.addActionListener(this);
        bp.setBounds(149,310,62,40);
            bp.addActionListener(this);
        bres.setBounds(211,310,62,40);
            bres.addActionListener(this);
        
        //Frame addings
        f.add(t1);
        f.add(bper);
        f.add(bce);
        f.add(bc);
        f.add(bba);
        f.add(b1x);
        f.add(bx2);
        f.add(b2x);
        f.add(bdiv);
        f.add(b7);
        f.add(b8);
        f.add(b9);
        f.add(bmul);
        f.add(b4);
        f.add(b5);
        f.add(b6);
        f.add(bsub);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(badd);
        f.add(bnp);
        f.add(b0);
        f.add(bp);
        f.add(bres);
        
        //Frame controls
        f.addWindowListener(this);
        f.setLayout(null);
        f.setSize(301,390);
        f.setTitle("Calculator");
        f.setVisible(true);
    }
    
    public void windowClosing(WindowEvent e)
    {
        f.dispose();
    }
    
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b0)
        {
            val=val+"0";
            t1.setText(val);
        }
        
        if(ae.getSource()==b1)
        {
            val=val+"1";
            t1.setText(val);
        }
        
        if(ae.getSource()==b2)
        {
            val=val+"2";
            t1.setText(val);
        }
        
        if(ae.getSource()==b3)
        {
            val=val+"3";
            t1.setText(val);
        }
        
        if(ae.getSource()==b4)
        {
            val=val+"4";
            t1.setText(val);
        }
        
        if(ae.getSource()==b5)
        {
            val=val+"5";
            t1.setText(val);
        }
        
        if(ae.getSource()==b6)
        {
            val=val+"6";
            t1.setText(val);
        }
        
        if(ae.getSource()==b7)
        {
            val=val+"7";
            t1.setText(val);
        }
        
        if(ae.getSource()==b8)
        {
            val=val+"8";
            t1.setText(val);
        }
        
        if(ae.getSource()==b9)
        {
            val=val+"9";
            t1.setText(val);
        }
        
        if(ae.getSource()==bp)
        {
            if(point==0)
            {
                val=val+".";
                t1.setText(val);
                point++;
            }
        }
        
        if(ae.getSource()==badd)
        {
            val=val+"+";
            t1.setText(val);
        }
        
        if(ae.getSource()==bsub)
        {
            val=val+"-";
            t1.setText(val);
        }
        
        if(ae.getSource()==bmul)
        {
            val=val+"*";
            t1.setText(val);
        }
        
        if(ae.getSource()==bdiv)
        {
            val=val+"/";
            t1.setText(val);
        }
        
        if(ae.getSource()==bc)
        {
            point=0;
        }
        
        if(ae.getSource()==bres)
        {
            int len=val.length()-1;
            int i=0;
            while(i<len)
            {
                if(ractive==0)
                {
                    if(Character.isDigit(val.charAt(i)) && opactive==0)
                    {
                        num1=num1+val.charAt(i);
                        n1active=1;
                    }
                    
                    if(val.charAt(i-inc)=='+' && n1active==1)
                    {
                        if(n1active==1 && n2active==0)
                        {
                            num2=num2+val.charAt(i);
                            inc++;
                            if(Character.isDigit(val.charAt(i))== false)
                            {
                                n2active=1;
                                lng=num2.length()-1;
                                num2=num2.substring(0,lng);
                            }
                        }
                        
                        if(n1active==1 && n2active==1)
                        {
                            res=Integer.parseInt(num1)+Integer.parseInt(num2);
                            ractive=1;
                            num2="";
                        }                       
                    }
                    
                    if(val.charAt(i-inc)=='-' && n1active==1)
                    {
                        if(n1active==1 && n2active==0)
                        {
                            num2=num2+val.charAt(i);
                            inc++;
                            if(Character.isDigit(val.charAt(i))== false)
                            {
                                n2active=1;
                                lng=num2.length()-1;
                                num2=num2.substring(0,lng);
                            }
                        }
                        
                        if(n1active==1 && n2active==1)
                        {
                            res=Integer.parseInt(num1)-Integer.parseInt(num2);
                            ractive=1;
                            num2="";
                        }
                    }
                    
                    if(val.charAt(i-inc)=='*' && n1active==1)
                    {
                        if(n1active==1 && n2active==0)
                        {
                            num2=num2+val.charAt(i);
                            inc++;
                            if(Character.isDigit(val.charAt(i))== false)
                            {
                                n2active=1;
                                lng=num2.length()-1;
                                num2=num2.substring(0,lng);
                            }
                        }
                        
                        if(n1active==1 && n2active==1)
                        {
                            res=Integer.parseInt(num1)*Integer.parseInt(num2);
                            ractive=1;
                            num2="";
                        }
                    }
                    
                    if(val.charAt(i-inc)=='/' && n1active==1)
                    {
                        if(n1active==1 && n2active==0)
                        {
                            num2=num2+val.charAt(i);
                            inc++;
                            if(Character.isDigit(val.charAt(i))== false)
                            {
                                n2active=1;
                                lng=num2.length()-1;
                                num2=num2.substring(0,lng);
                            }
                        }
                        
                        if(n1active==1 && n2active==1)
                        {
                            res=Integer.parseInt(num1)/Integer.parseInt(num2);
                            ractive=1;
                            num2="";
                        }
                    }
                }
                
                if(ractive==1)
                {
                    if(Character.isDigit(val.charAt(i)))
                    {
                        num2=num2+val.charAt(i);
                        n2active=1;
                    }
                    if(val.charAt(i)=='+')
                    {
                        res=res+Integer.parseInt(num2);
                        num2="";
                    }
                    if(val.charAt(i)=='-')
                    {
                        res=res-Integer.parseInt(num2);
                        num2="";
                    }
                    if(val.charAt(i)=='*')
                    {
                        res=res*Integer.parseInt(num2);
                        num2="";
                    }
                    if(val.charAt(i)=='/')
                    {
                        res=res/Integer.parseInt(num2);
                        num2="";
                    }
                }
                i++;
            }
            t1.setText(String.valueOf(res));
        }
    }
    
    public static void main(String args[])
    {
        calculator ob=new calculator();
        ob.Cal_UI();
    }
}
java awt
1个回答
0
投票

错误消息表明您正在尝试使用代码第 282 行的

""
方法将空字符串 (
Integer.parseInt
) 解析为整数。在解析之前,您应该检查您正在解析的
val
是否至少包含一个数字。

© www.soinside.com 2019 - 2024. All rights reserved.