0

I am trying to validate a string places in my HTML as a tag inside of my Java program so that if someone changes the HTML tag string my Java program checks the string if it is what I wrote.

Here is my java...

public void init()
{
    String name = getParameter("author");
    String course = getParameter("class");


    if (name == "goes here")
    JOptionPane.showMessageDialog (null, "Good, it will setVisible(true)", "Library System", JOptionPane.WARNING_MESSAGE);
    if (name != "goes here")
        JOptionPane.showMessageDialog (null, "NOT good and will setVisible(false)", "Library System", JOptionPane.WARNING_MESSAGE);

    Container contain = getContentPane();
    contain.add(new LibraryPanel(name, course));
    setVisible(true);
}

Here is my HTML...

<html>
<body>
<applet code=3.LibraryFrame.class width=650 height=700>
    <param name=author value="goes here">
    <param name=class value="className">
</applet>

</body>
</html>

So when my string in my HTML tag is correct however it always says it doesnt match. I even have placed the name and course variable in my showMessageDialog and it matches.

Any ideas on why it isn't correctly validating?

DDDD
  • 3,790
  • 5
  • 33
  • 55

2 Answers2

3

Always compare Strings with equals() and never with ==.

It sould be

if ("David Duke".equals(name))

because == compares references for reference data types.

Also you can get rid of that second if statement and use else for that.

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • I trying to test the results, I was using many if statements to see what was going on. – DDDD Nov 28 '12 at 07:56
3
if (name == "David Duke")

Never use == or != in Java to compare strings. Use equals() instead:

if (name.equals("David Duke"))

Note that == checks if the two arguments on the left and the right of it refer to the exact same object - it does not check if the contents of two objects are the same.

Jesper
  • 202,709
  • 46
  • 318
  • 350