Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use String.format() in Java? [duplicate]

I am a beginner in Java, and I'm using thenewboston's java tutorials (youtube). At tutorial 36-37 he starts using a String.format(); which he didn't explain in past tutorials. Here is the code for a class he was making:

    public class tuna {         private int hour;         private int minute;         private int second;          public void setTime(int h, int m, int s){             hour = ((h >= 0 && h < 24) ? h : 0);             minute = ((m >= 0 && m < 60) ? m : 0);             second = ((s >= 0 && s < 60) ? s : 0);         }          public String toMilitary(){             return String.format("%02d:%02d:%02d", hour, minute, second);         }     } 

So what he's doing is he's doing some sort of military time class and using String formatting. So what I'm asking is if someone can explain to me how String.format() works and how the formatting above works. Thanks for the help!

like image 377
Lemmy301 Avatar asked Mar 14 '14 22:03

Lemmy301


People also ask

What is string format () used for Java?

The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String. format() method, it uses the default locale by calling the Locale.

How do you repeat a string in Java?

repeated = new String(new char[n]). replace("\0", s); Where n is the number of times you want to repeat the string and s is the string to repeat.

What is %d and %s in Java?

%d means number. %0nd means zero-padded number with a length. You build n by subtraction in your example. %s is a string. Your format string ends up being this: "%03d%s", 0, "Apple"

How do I format a double string?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places. /* Code example to print a double to two decimal places with Java printf */ System.


2 Answers

It works same as printf() of C.

%s for String  %d for int  %f for float 

ahead

String.format("%02d", 8)

OUTPUT: 08

String.format("%02d", 10)

OUTPUT: 10

String.format("%04d", 10)

OUTPUT: 0010

so basically, it will pad number of 0's ahead of the expression, variable or primitive type given as the second argument, the 0's will be padded in such a way that all digits satisfies the first argument of format method of String API.

like image 151
Hiren Avatar answered Oct 18 '22 21:10

Hiren


It just takes the variables hour, minute, and second and bring it the the format 05:23:42. Maybe another example would be this:

String s = String.format("Hello %s answered your question", "placeofm"); 

When you print the string to show it in the console it would look like this

System.out.println(s); 

Hello placeofm answered your question

The placeholder %02d is for a decimal number with two digits. %s is for a String like my name in the sample above. If you want to learn Java you have to read the docs. Here it is for the String class. You can find the format method with a nice explanation. To read this docs is really important not even in Java.

like image 32
placeofm Avatar answered Oct 18 '22 19:10

placeofm