실행하면 다른 버튼들은 다 잘되는데 = 버튼은 누르면 아래 오류가 뜨는데 몇시간 째 해결책을 모르겠습니다..
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "9/3"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:651)
at NCalculator.calculate(NCalculator.java:139)
at NCalculator$PadActionListener.actionPerformed(NCalculator.java:77)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6616)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398)
at java.desktop/java.awt.Component.processEvent(Component.java:6381)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4991)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
------------코드 부분
public class NCalculator extends JFrame {
private JPanel panel;
private JTextField inputSpace;
private JButton[] buttons;
private String num ="";
private ArrayList<String> equation = new ArrayList<String>();
private String[] labels = {
"Backspace", "", "", "CE", "C",
"7", "8", "9", "/", "sqrt",
"4", "5", "6", "x", "%",
"1", "2", "3", "-", "1/x",
"0", "+/-", ".", "+", "=",
};
public NCalculator() {
inputSpace = new JTextField(35);
panel = new JPanel();
inputSpace.setText(" ");
inputSpace.setEnabled(false);
panel.setLayout(new GridLayout(0, 5, 3, 3));
buttons = new JButton[25];
int index = 0;
for (int rows = 0; rows < 5; rows++) {
for (int cols = 0; cols < 5; cols++) {
buttons[index] = new JButton(labels[index]);
if( cols >= 3 )
buttons[index].setForeground(Color.red);
else
buttons[index].setForeground(Color.blue);
buttons[index].setBackground(Color.yellow);
buttons[index].addActionListener(new PadActionListener());
panel.add(buttons[index]);
index++;
}
}
setTitle("계산기");
add(panel, BorderLayout.CENTER);
add(inputSpace, BorderLayout.NORTH);
setLocationRelativeTo(null);
setVisible(true);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class PadActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//어떤 버튼이 눌렸는지를 알아냄
String operation = e.getActionCommand();
//C가 눌렸을 경우 위의 계산식 내용을 지워줌
if (operation.equals("C")) {
inputSpace.setText("");
//=이 눌렸을 경우 위에 입력된 식을 계산, 계산값이 나오도록 함
} else if (operation.equals("=")) {
//밑의 메소드들을 이용하여 계산, 계산식 화면에 값을 띄워줌
String result = Double.toString(calculate(inputSpace.getText()));
inputSpace.setText("" + result);
num = "";
//나버지 버튼은 눌렀을 때 계산식에 추가됨
} else {
inputSpace.setText(inputSpace.getText() + e.getActionCommand());
}
}
}
private void fullTextParsing(String inputText) {
equation.clear();
//계산식의 글자를 하나하나 거쳐감
for (int i = 0; i < inputText.length(); i++) {
char ch = inputText.charAt(i);
//연산기호가 나오면 ArrayList에 추가되고 초기화
if (ch == '-' || ch == '+' || ch == '×' || ch == '÷') {
//연산기호를 만났다 : 앞은 숫자라는 것을 의미
//숫자를 ArrayList에 추가
equation.add(num);
//num 초기화
num = "";
//연산기호를 ArrayList에 추가
equation.add(ch + "");
} else {
//나머지는 그냥 숫자 처리
num = num + ch;
}
}
//반복문 끝나고 남아있는 숫자값 추가
equation.add(num);
equation.remove("");
}
//계산 기능
public double calculate(String inputText) {
fullTextParsing(inputText);
//위의 메소드를 실행하면 ArrayList에 숫자와 연산 기호가 담김
double prev = 0;
double current = 0;
//연산 기호에 대한 처리를 위한 변수
String mode = "";
//+일경우 add, -일경우 sub, x일경우 mul, /일경우 div
for (String s : equation) {
if (s.equals("+")) {
mode = "add";
} else if (s.equals("-")) {
mode = "sub";
}
else if (s.equals("×")) {
mode = "mul";
}
else if (s.equals("÷")) {
mode = "div";
} else {
//숫자일 경우 문자열을 Double로 형변환
current = Double.parseDouble(s);
//mode값에 따라 처리, prev는 계속 계산값이 갱신됨
if (mode.equals("add")) {
prev += current;
} else if (mode.equals("sub")) {
prev -= current;
}
else if (mode.equals("mul")) {
prev *= current;
}
else if (mode.equals("div")) {
prev /= current;
} else {
prev = current;
}
}
}
//계산값 prev 반환
return prev;
}
public static void main(String args[]) {
new NCalculator();
}
}