-1

Currently working on a simple system. Where the if else statement does not affect anything. Here's the code :

tollRate()
{

    JFrame a = new JFrame();
    setTitle("Highway Toll Rates System");
    setSize(600, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 2));

    p1.setLayout(new GridLayout(1, 2));
    p1.add(L1);                                             
    p1.add(t1);
    p2.setLayout(new GridLayout(1, 2));
    p2.add(L2);
    p2.add(t2);
    p3.setLayout(new GridLayout(1, 2));
    p3.add(L3);
    p3.add(t3);
    p4.setLayout(new FlowLayout());
    p4.add(b1);
    p4.add(b2);
    p5.setLayout(new FlowLayout());
    p5.add(L4);
    add(p1); add(p2); add(p3); add(p4); add(p5);

    b1.addActionListener(this);
b2.addActionListener(this);
}

public void actionPerformed(ActionEvent a) 
{
    Object source = a.getSource();
    if(source == b2) 
    {
          this.dispose();
    }
     else if(source == b1)
     {
        i = t1.getText();
        j = t2.getText();
        k = t3.getText();

        if (i!=SUNGAI BULOH)
        {
            JOptionPane.showMessageDialog(null, "Invalid Location" );
        }



     }








}

 public static void main(String[] args) 
{
    tollRate a = new tollRate();

}
}

Can anyone assist me with the if else statement in this code? I want the system to display "invalid location" if the user keys in anything other than "SUNGAI BULOH" in i. Thank you in advance.

  • There's a whole lot of undefined variables and some syntax errors, so it's difficult to tell what you're trying to do, but you're not trying to compare strings with `==` are you? See [this](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Bernhard Barker Aug 12 '13 at 15:41
  • You should spend some time to clean up your indentation. – recursive Aug 12 '13 at 15:42

4 Answers4

4

Alaways use String#equals() for comparing strings. Equals operator == only compares the references and may result in false even when two Objects' content is meaningfully equivalent.

   if (i!= null && !i.equals("SUNGAI BULOH")) // notice "quotes"
    {
        JOptionPane.showMessageDialog(null, "Invalid Location" );
    }

Also note that literal strings are always enclosed with "double" quotes in Java. Without them the compiler would actually treat them as Java identifiers.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
2

If you're performing a String compare, you should use this

if ( !"SUNGAI BULOH".equalsIgnoreCase(i) ){
   // do stuff
}

Since "equals" compares two strings, and "equalsIgnoreCase" does the same, ignoring case.

Cristian Meneses
  • 4,013
  • 17
  • 32
2

When comparing Objects in Java, == doesn't compare the value of the variables, it compares the reference. So the following would return false:

string s1 = "Hello";
string s2 = "Hello";
return s1==s2;

But this would return true:

string s1 = "Hello";
string s2 = s1;
return s1==s2;

Every Object has an equals() method that, for every subclass, should be overridden to compare the actual values in the object. So the following will return true:

string s1 = "Hello";
string s2 = "Hello";
return s1.equals(s2);

However, you have to be careful of null pointers - if s1 (in the above example) is null, it will throw an exception, so you should always first check for null, like this:

return s1 != null && s1.equals(s2);

Because of the way && works, the second half of the expression won't be evaluated if the first half is false. So it'll return false without hitting the null pointer exception.

Michelle
  • 2,830
  • 26
  • 33
1

I am not even sure how this compiles...

Here is what I would do to test your code:

public void actionPerformed(ActionEvent a)  {
    Object source = a.getSource();
    if(source == b2) {
          System.out.println("b1 selected");
          this.dispose();
    } else if(source == b1) {
        System.out.println("b2 selected");
        i = t1.getText();
        j = t2.getText();
        k = t3.getText();
        System.out.println("i text: " + i);

        if (!"SUNGAI BULOH".equalsIgnoreCase(i)) {
            JOptionPane.showMessageDialog(null, "Invalid Location" );
        }
     }

This will avoid null pointer exceptions if t1 contains a null value. Your variable names are pretty terrible.

Pete B.
  • 3,188
  • 6
  • 25
  • 38