What statement should I use to compare the characters in a string one by one so that it does not enter an infinite loop. Something like comparing an array of characters in C/C++.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Calcc extends JFrame implements ActionListener {
String l = "b";
int z = 0;
JFrame f;
JTextField t1 = new JTextField(30);
JTextField t2 = new JTextField(10);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("0");
JButton b11 = new JButton("+");
JButton b12 = new JButton("-");
JButton b13 = new JButton("*");
JButton b14 = new JButton("/");
JButton b15 = new JButton("=");
Calcc() {
f = new JFrame();
f.add(t1);
f.add(t2);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b10);
f.add(b11);
f.add(b12);
f.add(b13);
f.add(b14);
f.add(b15);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
f.setVisible(true);
f.setSize(350, 350);
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String d;
d = t1.getText();
if (e.getSource() == b1) {
t1.setText(d + "1");
}
if (e.getSource() == b2) {
t1.setText(d + "2");
}
if (e.getSource() == b3) {
t1.setText(d + "3");
}
if (e.getSource() == b4) {
t1.setText(d + "4");
}
if (e.getSource() == b5) {
t1.setText(d + "5");
}
if (e.getSource() == b6) {
t1.setText(d + "6");
}
if (e.getSource() == b7) {
t1.setText(d + "7");
}
if (e.getSource() == b8) {
t1.setText(d + "8");
}
if (e.getSource() == b9) {
t1.setText(d + "9");
}
if (e.getSource() == b10) {
t1.setText(d + "0");
}
if (e.getSource() == b11) {
t1.setText(d + "+");
}
if (e.getSource() == b12) {
t1.setText(d + "-");
}
if (e.getSource() == b13) {
t1.setText(d + "*");
}
if (e.getSource() == b14) {
t1.setText(d + "/");
}
if (e.getSource() == b15) {
while (l != "a") {
int b = calcA(d);
if (l == "+")
z = z + b;
else if (l == "-")
z = z - b;
else if (l == "*")
z = z * b;
else if (l == "*")
z = z * b;
}
t2.setText(String.valueOf(z));
}
}
int calcA(String d1) {
l = "a";
int k = 0;
while ((d1 != "+") && (d1 != "-") && (d1 != "*") && (d1 != "/")) {
if (d1 != "\n")
break;
k = k * 10 + Integer.valueOf(d1);
}
l = d1;
return k;
}
}
public class CalcD {
public static void main(String[] args) {
new Calcc();
}
}