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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful, I just tried .equalsignorecase() and it works now.
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.

1 Comment

I trying to test the results, I was using many if statements to see what was going on.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.