1

I can't figure out how to use variables in HTML, string is a variable in this case.

JOptionPane.showMessageDialog(null,"<html>Error #1<br> + string +</html>","Error",JOptionPane.PLAIN_MESSAGE);

this output: Error #1 + string

JOptionPane.showMessageDialog(null,"<html>Error #1<br></html>" + string ,"Error",JOptionPane.PLAIN_MESSAGE);

this output: Error #1

is there a way to use string variables in HTML?

1
  • 1
    JOptionPane.showMessageDialog(null,"<html>Error #1<br>" + string + "</html>","Error", Commented Nov 27, 2013 at 10:22

3 Answers 3

2

Do you want something like

"<html>Error #1<br>" + string + "</html>"

? If you want to concatenate the string variable to the html you have there, it must be outside the quotes, otherwise it will be treated as a literal, as in your example.

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

Comments

2
JOptionPane.showMessageDialog(
    null,
    "<html>Error #1<br>" + string + "</html>",
    "Error",
    JOptionPane.PLAIN_MESSAGE);

Though note that logically JOptionPane.PLAIN_MESSAGE should be JOptionPane.ERROR_MESSAGE.

Comments

2

You need to do: "<html>Error #1<br>" + string + "</html>"

A string literal in java consists of zero or more characters enclosed in double quotes. So anything satisfies this condition will also be regarded as string too. You will need to close the String before appending the string.

So if the string = "Hi to stack":

Then "<html>Error #1<br>" + string + "</html>" will result in:

"<html>Error #1<br>Hi to stack</html>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.