String Formatter
public class Main {
public static void main(String[] args) {
System.out.println("1");
printInformation("Hello, World");
System.out.println("2");
printInformation("");
System.out.println("3");
printInformation();
System.out.println("4");
printInformation("\t \n");
}
public static void printInformation() {
System.out.println("Overload method: String is empty\n");
}
public static void printInformation(String string) {
int length = string.length();
if (string.isEmpty()) {
System.out.println("String is empty\n");
}
if (string.isBlank()) {
System.out.println("String is blank\n");
}
if (length > 0) {
System.out.printf("Length = %d %n", length);
System.out.printf("First char = %c %n", string.charAt(0));
System.out.printf("Last char = %c %n", string.charAt(length - 1));
for (int x = 0; x < length; x++) {
System.out.printf("Position: %d : %c %n", x, string.charAt(x));
}
String helloWorld = "Hello, World";
System.out.printf("index of , is: %d %n", helloWorld.indexOf(','));
System.out.printf("index of World is: %d %n", helloWorld.indexOf("World"));
System.out.printf("index of l is: %d %n", helloWorld.indexOf("l"));
System.out.printf("index of l is: %d %n", helloWorld.lastIndexOf("l"));
System.out.printf("index of l is: %d %n", helloWorld.indexOf("l", 3));
System.out.printf("index of l is: %d %n", helloWorld.lastIndexOf("l", 8));
//String compare values
String helloWorldLower = helloWorld.toLowerCase();
if (helloWorld.equals(helloWorldLower)) {
System.out.println("Values match exactly");
}
if (helloWorld.equalsIgnoreCase(helloWorldLower)) {
System.out.println("Values match ignoring case");
}
if (helloWorld.startsWith("Hello")){
System.out.println("Starts with Hello");
}
if (helloWorld.endsWith("World")) {
System.out.println("Ends with World");
}
if (helloWorld.contains("World")) {
System.out.println("Contains World");
}
}
}
}
1
Length = 12
First char = H
Last char = d
Position: 0 : H
Position: 1 : e
Position: 2 : l
Position: 3 : l
Position: 4 : o
Position: 5 : ,
Position: 6 :
Position: 7 : W
Position: 8 : o
Position: 9 : r
Position: 10 : l
Position: 11 : d
index of , is: 5
index of World is: 7
index of l is: 2
index of l is: 10
index of l is: 3
index of l is: 3
Values match ignoring case
Starts with Hello
Ends with World
Contains World
2
String is empty
String is blank
3
Overload method: String is empty
4
String is blank
Length = 4
First char =
Last char =
Position: 0 :
Position: 1 :
Position: 2 :
Position: 3 :
index of , is: 5
index of World is: 7
index of l is: 2
index of l is: 10
index of l is: 3
index of l is: 3
Values match ignoring case
Starts with Hello
Ends with World
Contains World
Process finished with exit code 0
public class Main {
public static void main(String[] args) {
System.out.println("Pre-Java 15 method of bullet points");
String bulletIT = "Print a Bulleted List:\n" +
"\t\u2022 First Point\n" +
"\t\t\u2022 Sub Point\n";
System.out.println(bulletIT);
System.out.println("Text Block");
// 3 double quotes on own line
String textBlock = """
Print a Bulleted List:
\u2022 First Point
\u2022 Sub Point
""";
System.out.println(textBlock);
int age = 35;
System.out.printf("Your age is %d\n", age);
int currentYear = 2003 - age;
System.out.printf("Your age is %d, Birth year is: %d%n", age, currentYear);
for (int x = 1; x < 1000000; x *= 10) {
System.out.println(x);
}
// use /\n to skip line not %n
System.out.println("%n");
System.out.println("\n");
System.out.println("Right Justify the numbers");
for (int x = 1; x < 1000000; x *= 10) {
System.out.printf("%6d %n", x);
}
System.out.println("\n");
String formattedString = String.format("Your age is %d %n", age);
System.out.println(formattedString);
formattedString = "Your age is %d".formatted(age);
System.out.println(formattedString);
}
}
Pre-Java 15 method of bullet points
Print a Bulleted List:
• First Point
• Sub Point
Text Block
Print a Bulleted List:
• First Point
• Sub Point
Your age is 35
Your age is 35, Birth year is: 1968
1
10
100
1000
10000
100000
%n
Right Justify the numbers
1
10
100
1000
10000
100000
Your age is 35
Your age is 35
Process finished with exit code 0