mkwilfreid
mkwilfreid

Reputation: 128

formatting number JOptionPane

Good day, i'm facing the following problem. i'm generating a random number which i need to display all along some text on a JOptionPane. if i were to use a JLabel, i could have just added the JLabel to the JOptionPane straight forward. but i don't wanna make use of it. and i'm using the html tags, rather.

My question is the following, how will i get %d to get the number that i'm generating to be displayed?

with programming languages like java, i can print by doing:

System.out.printf("%d", +number);

System.out.printf(%d +number);

How will i go about with html, from the bellow code?

int number = 20140000+ generator.nextInt(999);

String msg = "<html><h3><b>You have been successfully registered into MYCARE.<br><br>Your Patient ID is: %d. Please use it to login.</br></br></b></h3></html>" +number;
JOptionPane.showMessageDialog(null, msg, "INFORMATION", JOptionPane.INFORMATION_MESSAGE);

Any help will be gladly appreciated.

Upvotes: 0

Views: 98

Answers (2)

huidube
huidube

Reputation: 410

The normal System.out.printf() method works like this:

int number = 5;
System.out.printf("%s %d", "aString", number);  //output: "aString 5"

What you want, is to use this in a String. You can use String.format() to put values into a String:
Example:

String s = String.format("<html><h3><b>You have been successfully registered into MYCARE.<br><br>Your Patient ID is: %d. Please use it to login.</br></br></b></h3></html>",number;);

Upvotes: 0

Aleksandr Podkutin
Aleksandr Podkutin

Reputation: 2580

In your code you just concatenate number and string. Try String.format():

String msg = String.format("<html><h3><b>You have been successfully registered into MYCARE.<br><br>Your Patient ID is: %d. Please use it to login.</br></br></b></h3></html>", number);

Upvotes: 1

Related Questions