-4

Trying to figure out what's wrong with my code here - I want user input to be checked with the concentrations array and if they enter any of the array to print out a positive display message... However, I get an output of "Yes, that is a valid concentration" every time, regardless of input.

import javax.swing.JOptionPane;
public class BASIT_Concentration_Check
{
    public static void main(String[] args) 
    {
        String[] concentrations = {"DTP","HCIT","INFS","NTEL","WDM"};
        String studentconcentration = getStudentConcentration();
        boolean concentrationvalid = isConcentrationValid(studentconcentration, concentrations);
        displayMessage(concentrationvalid);
    }

    public static String getStudentConcentration()
    {
        String studentconcentration = JOptionPane.showInputDialog(null,"What is your B.S. AIT Concentration?");
        return studentconcentration;
    }

    public static boolean isConcentrationValid(String studentconcentration, String [] concentrations)
    {
        boolean concentrationvalid=false;
        for (int i=0;i<concentrations.length;i++)
        {
            if (concentrations[i]==studentconcentration)
                {
                    concentrationvalid = true;
                }
        }
        return concentrationvalid;
    }

    public static void displayMessage(boolean concentrationvalid)
    {
        if (concentrationvalid==true)
        {
            JOptionPane.showMessageDialog(null, "Yes, that is a valid concentration");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"I'm sorry, that is not a valid concentration");
        }
    }
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

3 Answers3

1

if (concentrations[i]==studentconcentration)

That is not how you compare strings in Java!

Instead use if (concentrations[i].equals(studentconcentration))

Basically, == only compares references (or values for primitive types), for a more detailed description of why it is this way, see this question

Community
  • 1
  • 1
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • This doesn't explain why he's getting "Yes, that is valid" every time. It would explain why he would get "Its not valid" every time, but that's not the case. – Nathan Merrill Nov 02 '13 at 02:29
  • @MrTi That's true, in that case the problem is not reproducible. Which means that the OP has most likely not copied **the code, all the code and nothing but the code.** or not given enough details. Or mistakenly wrote the wrong message in the comment. – Simon Forsberg Nov 02 '13 at 11:46
0

You have a comparison error. In Java, using == operator fails with strings since strings are not primitive types, they are objects. So you have to use the equals method of the String class, like this.

if (concentrations[i].equals(studentconcentration))

This should work. Also you can add a break; statement if the validation is true to save some cpu processing. One more thing, there is no need to add == true in the if comparison of booleans, you can just use

if (concentrationvalid)

The above statement is the same as with your statement but is more readable.

Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
0

You would need to use .equals() to compare strings in Java. It will return a boolean value of true or false which would activate the if statement if true or skip it if false.

You can also use .equalsIgnoreCase() if you want the comparison to ignore whether the letters are upper/lower case.

Ex:

if (concentrations[i].equals(studentconcentration))

or

if (concentrations[i].equalsIgnoreCase(studentconcentration))

Hope that helps!

David Baez
  • 1,208
  • 1
  • 14
  • 26