【Java】エラーの意味・直し方
こんにちは。当方java初心者です。
eclipseで電卓のプログラムを作ってみたのですが、コンパイルする際に
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14
at Keypad.<init>(Calculator.java:81)
at Calculator.<init>(Calculator.java:24)
at Calculator.main(Calculator.java:39)
とエラーがでました。
81、24、39行目に何かミスがあったのかな?と思いましたが、自分ではどこが間違っているのか全くわからない状態です・・・
・なにが間違っているのか
・どうしたらコンパイルが通るのか
の、2点をお聞きしたいです。
初心者ゆえわかりにく文章での質問申し訳ありませんが、回答してくださると嬉しいです。
ソースです。↓
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Calculator extends Frame
{
private Display out;
private Keypad in;
private Maths sums;
public Calculator()
{
setLayout(new BorderLayout());
out = new Display();
sums = new Maths(out);
in = new Keypad(out);
add("North", out);
add("Center", in);
add("East", sums);
this.addWindowFocusListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
Frame f = new Calculator();
f.setSize(100, 200);
f.setVisible(true);
}
}
class Display extends Panel
{
private TextField show;
public Display()
{
show = new TextField(10);
Panel p = new Panel();
p.add(show);
add("Center", p);
}
public void write(String s)
{
show.setText(s);
}
public String read()
{
return show.getText();
}
}
class Keypad extends Panel implements ActionListener
{
private Display monitor;
public Keypad(Display myMonitor)
{
monitor = myMonitor;
Panel p = new Panel();
p.setLayout(new GridLayout(5, 3));
String[] buttonLabels = {"0","1","2","3","4","5","6","7","8","9",".","","","C"};
Button[] buttons = new Button[15];
for (int i = 0; i <= 14; i++)
{
buttons[i] = new Button(buttonLabels[i]);
buttons[i].addActionListener(this);
p.add(buttons[i]);
}
add("Center", p);
}
public void actionPerformed(ActionEvent e)
{
char c = e.getActionCommand().charAt(0);
switch(c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
monitor.write(monitor.read()+c);
break;
case 'C':
monitor.write("");
break;
}
}
}
class Maths extends Panel implements ActionListener
{
private Display monitor;
private double num1, num2, result;
private char operand;
public Maths(Display myMonitor)
{
monitor = myMonitor;
Panel p = new Panel();
p.setLayout(new GridLayout(5, 1));
String[] buttonLabels = {"+","-","*","/","="};
Button[] buttons = new Button[5];
for (int i = 0; i <= 4; i++)
{
buttons[i] = new Button(buttonLabels[i]);
buttons[i].addActionListener(this);
p.add(buttons[i]);
}
add("North", p);
}
public void actionPerformed(ActionEvent e)
{
char c = e.getActionCommand().charAt(0);
switch(c)
{
case '+':
case '*':
case '-':
case '/':
num1 = Double.valueOf(monitor.read()).doubleValue();
operand = c;
monitor.write("");
break;
case '=':
num2 = Double.valueOf(monitor.read()).doubleValue();
result = sum(num1, num2, operand);
monitor.write(String.valueOf(result));
}
}
public double sum(double x, double y, char z)
{
double answer = 0;
if(z == '+')answer = x+y;
else if(z == '-') answer = x-y;
else if(z == '*') answer = x*y;
else if(z == '/') answer = x/y;
return answer;
}
}
お礼
非常に勉強になりました! ありがとうございました。