Here is the syntax of the method:
public int length()
Note that there is no parameter and that the method is not static. This means that the method can only be invoked through a Java String instance.
The return value of the method is an int that represents the number of characters the String contains.
Here is an example Java class that reads an input from the command line and displays the length of the given string.
public class Test { public static void main(String[] args) { System.out.print("Please enter a string: "); String stringInput = System.console().readLine(); int stringLength = stringInput.length(); System.out.println("The number of characters is: " + stringLength); } }
Here is an example run of the program:
Please enter a string: testing123 The number of characters is: 10
This sample will read an input String and use the String length to loop through all it's characters.
public class Test { public static void main(String[] args) { System.out.print("Please enter a string: "); String stringInput = System.console().readLine(); int stringLength = stringInput.length(); for (int charIndex = 0; charIndex < stringLength; charIndex++) { char c = stringInput.charAt(charIndex); System.out.println(c); } } }
Here is a sample run of the program:
Please enter a string: testing t e s t i n g
This example will read two input String from the user. The length o f the two strings will be compared to show which is longer.
public class Test { public static void main(String[] args) { System.out.print("Please enter first string: "); String firstString = System.console().readLine(); System.out.print("Please enter second string: "); String secondString = System.console().readLine(); // int firstStringLength = firstString.length(); int secondStringLength = secondString.length(); if (firstStringLength > secondStringLength) { System.out.println("The first string is longther than the second string"); } else if (secondStringLength > firstStringLength) { System.out.println("The second string is longther than the first string"); } else { System.out.println("The first and second string have equal lengths"); } } }Here are sample runs of the program:
Please enter first string: abc Please enter second string: defg The second string is longther than the first string
Please enter first string: apple Please enter second string: cider The first and second string have equal lengths
The length can also be used to validate that the given string should not exceed some limit.
public class Test { public static void main(String[] args) { System.out.print("Please enter a string: "); String stringInput = System.console().readLine(); int stringLength = stringInput.length(); if (stringLength > 10) { System.out.println("Input string should not exceed 10 characters"); } else { System.out.println("The input string is valid."); } } }
Here are sample runs of the program:
Please enter a string: thequickbrown Input string should not exceed 10 characters
Please enter a string: apple The input string is valid.
This example will keep on reading for user input until the user enters a non empty String.
public class Test { public static void main(String[] args) { String stringInput = ""; while (stringInput.length() == 0) { System.out.print("Please enter a string: "); stringInput = System.console().readLine(); } System.out.println("You entered the string: " + stringInput); } }
Here is a sample run of the program:
Please enter a string: Please enter a string: Please enter a string: Please enter a string: test You entered the string: test