-1

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();
    }

}
anacron
  • 6,443
  • 2
  • 26
  • 31

3 Answers3

1

You do not need to check with any character like you do in C/C++. You need to simply iterate it with the length of the String. Simply use this in your loop:

for (int i=0;i<string.length();i++) {
    char c = string.charAt(i);
    // do whatever with your character.
}

That said, when you compare strings for equality in Java, you should always use the String.equals method instead of ==.

So, you should change your code from

getSource() == b1

to

getSource().equals(b1)

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
1

You appear to be comparing strings alot, using == to do it is bad practice.

This is because Strings are objects and comparing using the == operator will check to see if they are the exact same object regardless of value. Strings are a special case, when creating a String the jvm will decide whether or not to use an existing object or create a new one, so we can not be 100% sure if "==" will return true or not even the they contain the same value.

To compare Strings please use string1.equals(string2).

The .equals method should be used to compare the value of objects, and the == operator to compare primitives.

MartinByers
  • 1,240
  • 1
  • 9
  • 15
  • srry my code is wrong there....actually i wanted to compare every single character of the string to the given character....but thank you for responding Mr. MartinByers – EzhilKumaran Jun 27 '17 at 11:04
  • You can compare chars using the == operator. Just incase you were not aware a String has a toCharArray() method. Which may help you. – MartinByers Jun 27 '17 at 21:49
0

Thank you guys for clarifying some doubts.... for the above programme i used String.charAt(int); to compare every specific character at the loop....