-1

This is one of my first java programs, which includes a GUI and an linear array (I hope!)

I know this isn't suppose to be a 'help' website but more of a place to collaborate ideas, but I've been staring my eyes out for the past hour and a half with little to no progress on my problem (which I will get too momentarily) The funny being it's probably not even the hardest bit of the program.

So I'm hoping you guys can help me figure this out. I've created a fairly simple program that most I've already written, and after completion of the program the user can input a number in (enterNumberField) choosing numbers from the linearArray (1, 2, 31 or 35) and after pressing a button, the program outputs either A, B, C or D in the textField I've dubbed 'ItsAField' (creative I know).

Unfortunately, no matter what number is chosen it always outputs as D.

public class ChildrensClassicsView extends FrameView {

    String [] linearArray = {"1", "2", "31", "35"}; //numbers to choose from

}

    static public Boolean linearSearch (String [] A, String B) {
        for (int k=0; k<A.length; k++) {
            if (A[k].equals(B)) {
                return true;
            }
        }
        return false;
    } 

After pressing action performed button...

It wouldn't surprise me if it was just a problem with these 'if statements' however I'm not even sure if you can use them like this to get the answer I'm looking for. Am I on the right track for outputting the 'chosen' variable?

    private void findButtonActionPerformed(java.awt.event.ActionEvent evt) { 

String input = enterNumberField.getText();

if (input.equals("1")) {
        ItsAField.setText("Search: A " + linearSearch(linearArray, "1"));
    }
    if (input.equals("2")) {
        ItsAField.setText("Search: B " + linearSearch(linearArray, "2"));
    }
    if (input.equals("28")){
        ItsAField.setText("Search: C " + linearSearch(linearArray, "31"));
    } else {
        ItsAField.setText("Search: D " + linearSearch(linearArray, "35"));
    }

I also have my figures crossed hoping this still qualifies as a real question :)

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Luuklag Jul 23 '19 at 09:39

2 Answers2

0

You need to compare strings with String.equals() not ==

So change the if statements to if("1".equals(input))

FDinoff
  • 30,689
  • 5
  • 75
  • 96
0

You are attempting to compare String values with the == operator, but in Java, for objects (including Strings), this compares the object references to determine if they refer to the same object.

To compare String values, use String#equals.

if (input.equals("1")) {

EDIT

The question changed. It used to be an if followed by 2 else-ifs followed by an else, with each one using == attempting to compare String values.

Now it is 3 if statements, with only the last one having an else, even if they're all using equals now.

Restore the else before the 2nd and 3rd if statements. They should be else if statements.

if (input.equals("1")) {
    ...
}
else if (input.equals("2")) {  // else restored here
    ...
}
else if (input.equals("28")){  // else restored here
    ...
} else {
    ...
}

This way, exactly one conditional block is executed.

rgettman
  • 176,041
  • 30
  • 275
  • 357