-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringFormatting.java
More file actions
31 lines (22 loc) · 988 Bytes
/
StringFormatting.java
File metadata and controls
31 lines (22 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class StringFormatting {
public static void main(String[] args) {
//printf
int yearBorn = 2000;
double weightKg = 120.129;
String firstName = "Tiger";
System.out.printf("The golfer %s weighs %.2f kg and was born in %d!!!",
firstName, weightKg, yearBorn);
//sysout string using the printf method. ("string here", variables)
//("The golfer %s weighs %.2f kg and was born in %d!!!", firstName, weightKg, yearBorn);
//%s uses the declared String variable(firstName)
//%.2f uses the declared double variable(weightKG)
//note that the .2 in $.2f declares the int's after the radix point ie 0.22
//%d uses the declared int variable(yearBorn)
//%n creates a line break
//Put a negative sign after the % to left-justify the text.
//Precede the letter s with a number to specify field width.
String s;
s = String.format("The golfer %s weighs %.2f kg and was born in %d!!!", firstName, weightKg, yearBorn);
System.out.println(s);
}
}