另一个初学者计算器,String to int

时间:2015-03-06 08:31:29

标签: java

我也试图从头开始构建一个计算器并在SO上搜索了很多类似的主题,但是没有它们似乎可以解决我的问题。

问题: 当我按下结果按钮时,我得到了

  

'线程中的异常" AWT-EventQueue-0"   java.lang.NumberFormatException:null'   线程中的异常" AWT-EventQueue-0" java.lang.NumberFormatException:null     在java.lang.Integer.parseInt(未知来源)at   java.lang.Integer.parseInt(未知来源)at   com.Bob.Rekenmachine $ isListener.actionPerformed(Rekenmachine.java:302)     在javax.swing.AbstractButton.fireActionPerformed(未知来源)at   javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at   javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at   javax.swing.DefaultButtonModel.setPressed(未知来源)at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知   来自)java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Window.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 500(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来源)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 4.run(未知来源)at   java.awt.EventQueue $ 4.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

我的问题 我不确定如何显示计算结果。从String到INT的转换似乎失败了

我的代码:

import java.util.*;
import java.util.List;
import java.awt.event.*;

import javax.swing.*;

import java.awt.*;
import java.io.*;


public class Rekenmachine {
JButton deelknop;
JButton maalknop;
JButton minknop;
JButton plusknop;
JButton isknop;
JTextArea display;                              
JButton num1;
JButton num2;
JButton num3;
JButton num4;
JButton num5;
JButton num6;
JButton num7;
JButton num8;
JButton num9;
JButton nume1;
JButton num0;
JButton nume2;
String result1;
int result11;
int result22;
int uitkomst00;
String result2;
boolean deelknopkeus = false;
boolean plusknopkeus = false;
boolean minknopkeus = false;
boolean maalknopkeus = false;
String uitkomst;

public static void main(String[] args) {
    Rekenmachine r = new Rekenmachine();
    r.go();
}

public void go() {
    JFrame frame = new JFrame("Rekenmachine");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel mainPanel = new JPanel();
    JPanel buttons = new JPanel();
    JPanel cijfers = new JPanel();
    buttons.setBackground(Color.darkGray);
    buttons.setLayout(new BoxLayout (buttons, BoxLayout.Y_AXIS));
    GridLayout cijferlayout = new GridLayout(4,3);
    GridLayout buttonslayout = new GridLayout(5,1);
    buttons.setLayout(buttonslayout);
    cijfers.setLayout(cijferlayout);
    cijfers.setBackground(Color.CYAN);

    display = new JTextArea(1, 15);
    display.setFont(new Font("Serif",Font.BOLD, 30));
    display.getCursor();                                                
    display.setToolTipText("Vul hier uw waarden is");
    display.setLineWrap(true);                                          
    display.setWrapStyleWord(false);






    JButton deelknop = new JButton("/");
    deelknop.setFont(new Font("Serif",Font.BOLD, 30));
    deelknop.addActionListener(new deelListener());

    JButton maalknop = new JButton("*");
    maalknop.setFont(new Font("Serif",Font.BOLD, 30));
    maalknop.addActionListener(new maalListener());

    JButton minknop = new JButton("-");
    minknop.setFont(new Font("Serif",Font.BOLD, 30));
    minknop.addActionListener(new minListener());

    JButton plusknop = new JButton("+");
    plusknop.setFont(new Font("Serif",Font.BOLD, 30));
    plusknop.addActionListener(new plusListener());

    JButton isknop = new JButton("=");
    plusknop.setFont(new Font("Serif",Font.BOLD, 30));
    isknop.addActionListener(new isListener());

    JButton num1 = new JButton("1");
    num1.setFont(new Font("Serif",Font.BOLD, 30));   
    num1.addActionListener(new num1Listener());


    JButton num2 = new JButton("2");
    num2.setFont(new Font("Serif",Font.BOLD, 30));
    num2.addActionListener(new num2Listener());

    JButton num3 = new JButton("3");
    num3.setFont(new Font("Serif",Font.BOLD, 30));
    num3.addActionListener(new num3Listener());

    JButton num4 = new JButton("4");
    num4.setFont(new Font("Serif",Font.BOLD, 30));
    num4.addActionListener(new num4Listener());

    JButton num5 = new JButton("5");        
    num5.setFont(new Font("Serif",Font.BOLD, 30));
    num5.addActionListener(new num5Listener());

    JButton num6 = new JButton("6");
    num6.setFont(new Font("Serif",Font.BOLD, 30));
    num6.addActionListener(new num6Listener());

    JButton num7 = new JButton("7");
    num7.setFont(new Font("Serif",Font.BOLD, 30));
    num7.addActionListener(new num7Listener());

    JButton num8 = new JButton("8");
    num8.setFont(new Font("Serif",Font.BOLD, 30));
    num8.addActionListener(new num8Listener());

    JButton num9 = new JButton("9");
    num9.setFont(new Font("Serif",Font.BOLD, 30));
    num9.addActionListener(new num9Listener());

    JButton nume1 = new JButton();
    nume1.setFont(new Font("Serif",Font.BOLD, 30));

    JButton num0 = new JButton("0");
    num0.setFont(new Font("Serif",Font.BOLD, 30));
    num0.addActionListener(new num0Listener());

    JButton nume2 = new JButton();
    nume2.setFont(new Font("Serif",Font.BOLD, 30));


    cijfers.add(num1);
    cijfers.add(num2);
    cijfers.add(num3);
    cijfers.add(num4);
    cijfers.add(num5);
    cijfers.add(num6);
    cijfers.add(num7);
    cijfers.add(num8);
    cijfers.add(num9);
    cijfers.add(nume1);
    cijfers.add(num0);
    cijfers.add(nume2);


    mainPanel.add(display);
    buttons.add(deelknop);
    buttons.add(maalknop);
    buttons.add(minknop);
    buttons.add(plusknop);
    buttons.add(isknop);

    frame.getContentPane().add(BorderLayout.CENTER, cijfers);
    frame.getContentPane().add(BorderLayout.NORTH, mainPanel);
    frame.getContentPane().add(BorderLayout.EAST, buttons);
    frame.setSize(500, 500);
    frame.setVisible(true);





} class num1Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            result1 = display.getText(); 
            display.setText(result1 + "1");
            result1 = display.getText();                             


}
}
class num2Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        result1 = display.getText(); 
        display.setText(result1 + "2");
        result1= display.getText();                             




 }
   }
    class num3Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "3");
            result1= display.getText();                             


}
}
    class num4Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "4");
            result1= display.getText();                             


}
}
    class num5Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "5");
            result1= display.getText();                             


}
}
    class num6Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "6");
            result1= display.getText();                             


}
}
    class num7Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "7");
            result1= display.getText();                             


}
}
    class num8Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "8");
            result1= display.getText();                             


}
}
    class num9Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "9");
            result1= display.getText();                             


}
}
    class num0Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            display.setText(result1 + "0");
            result1= display.getText();                             


}
}
    class plusListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            result1 = display.getText(); 
            plusknopkeus = true;
            display.setText("");

    }   
    }
class maalListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        maalknopkeus = true;
        display.setText("");                
    }   
    }

class deelListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        deelknopkeus = true;
        display.setText("");                
    }   
    }

class minListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        minknopkeus = true;
        display.setText("");                
    }   
    }
class isListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {


        result2 = display.getText();

        if ( plusknopkeus == true ) 
        result11 = Integer.parseInt(result1);
        result22 = Integer.parseInt(result2);
        uitkomst00 = Integer.parseInt(uitkomst);
        uitkomst00 = result22 + result11;



        display.setText(uitkomst00);

    }
}

我的代码行293-303:

    class isListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {


        result2 = display.getText();

        if ( plusknopkeus == true ) 
        result11 = Integer.parseInt(result1);
        result22 = Integer.parseInt(result2);
        uitkomst00 = Integer.parseInt(uitkomst);
        uitkomst00 = result22 + result11;

额外信息

我知道我的代码可以小得多,使用循环等。我只是弄清楚如何让我的计算器工作然后我会整理一下。我希望代码对你来说是可读的。

1 个答案:

答案 0 :(得分:0)

我更改了您的事件处理程序并创建了一个额外的方法resetOprations()

class plusListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        result2 = display.getText();
        resetOprations();
        plusknopkeus = true;
        result22 = Integer.parseInt(result2);
        display.setText("");

    }
}

class maalListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        result2 = display.getText();
        resetOprations();
        maalknopkeus = true;
        result22 = Integer.parseInt(result2);
        display.setText("");
    }
}

class deelListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        result2 = display.getText();
        resetOprations();
        deelknopkeus = true;
        result22 = Integer.parseInt(result2);
        display.setText("");
    }
}

class minListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        result2 = display.getText();
        resetOprations();
        minknopkeus = true;
        result22 = Integer.parseInt(result2);
        display.setText("");
    }
}

public void resetOprations() {
    deelknopkeus = false;
    plusknopkeus = false;
    minknopkeus = false;
    maalknopkeus = false;
}

class isListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // result2 = display.getText();
        result11 = Integer.parseInt(result1);

        display.setText(result22 + "");
        if (plusknopkeus) {
            result22 = result22 + result11;
        } else if (minknopkeus) {
            result22 = result22 - result11;
        } else if (maalknopkeus) {
            result22 = result22 * result11;
        }else if (deelknopkeus) {
            result22 = result22 / result11;
        }
        display.setText(result22 + "");

    }
}