Java program to calculate length of string – In this article, we will discuss the various methods to calculate the length of a string, str length in Java programming. String length() java has been written in 3 to 4 different ways, you can check out here. If you any queries about source code str length in java, leave a comment here.
The methods used in this article are as follows:
- Using Standard Method
- Using Scanner Class (Predefined method)
- Using Scanner Class
- Using Static Method
- Using Separate Class
A string is a data type used in programming like an integer or a floating point but it is used to represent text whenever it is required instead of numerical.
As you can see, the string mentioned here is “Hello”. The string consists of 5 characters. All of the 5 characters are represented by a pointer. The location is represented by a pointer, in this case, starting from 0 to 4.
Similarly, other strings also have ideal lengths where the space between two words is also counted.
str length in Java – Using Standard Method
To find the length to string, we first taken in a string input in the code itself.
This string input is converted to a character array so as to traverse to the end of string individual character-wise to find the length.
To make this conversion, we can make use of a built in method for strings as below:
1 |
char[] len= str.toCharArray(); |
In this character array, we traverse until the end of the array in a loop and keep incrementing the count which is to be stored in a separate integer variable (a).
The end value of this variable (a) is nothing but, the length of our input string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Scanner; class StringLength { public static void main(String x[]) { Scanner sc=new Scanner(System.in); String str="Hello World"; char[] len= str.toCharArray(); int a=0; for(char ch : len) { a++; } System.out.println("Length of a string is :"+a); } } |
Output:
1 |
Length of a string is :11 |
Java String Length – Predefined Method
In the above example, we had given inputs manually and predetermined in the code itself and if any changes in value were to be made, the user has to go to the code and make changes.
To avoid this, Scanner class in Java can be made use of.
With the help of Scanner class any primitive type inputs can be read at runtime.
So, for our problem, we will read a string input. String in Java has many predefined methods in it. T
By making use of this predefined method and giving our input string as the String whose length is to be found, we can directly display the length in our output screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Scanner; class StringLength { public static void main(String x[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a string :"); String str=sc.nextLine(); int len=str.length(); System.out.println("string contains "+len+" characters); } } |
Output:
1 2 3 |
Enter a string : very good morning string contains 17 characters |
Using Scanner Class
In here, just like the explanation given above to make use of scanner class to read inputs at runtime, the same method can be used to read our input.
After getting the input, instead of using a predefined method, we can make use of the logic discussed in the beginning by converting the string to a character array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class StringLength { public static void main(String x[]) { int c=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a string :"); String str=sc.nextLine(); char[] a= str.toCharArray(); for(char ch : a) { c++; } System.out.println("string contains "+c+" characters"); } } |
Output:
1 2 3 |
Enter a string : hi how are you string contains 14 characters |
Using Static Method
This technique is made use of for better readability. We have split up the code into two methods in the same class.
The main method is responsible for all the input and output operations. Input is taken in with the help of Scanner class.
After taking input, a separate static method (length) consisting of the set of statements for calculation of length of string is called.
This method (length) converts individual alphabet to character and increments the count to finally find the total length of the string.
After executing all the statements in it, returns the length of the string. The main logic is enclosed in a try block so that we can catch if any exception occurs like a null pointer, out of bound, etc.
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 |
import java.util.Scanner; class StringLength { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a string :"); String str=sc.nextLine(); int res=length(str); System.out.println("string contains "+res+" characters"); } static int length(String s) { int j=0; try { for ( j = 0;;j++) s.charAt(j); } catch (Exception e) { } return j; } } |
Output:
1 2 3 |
Enter a string : welcome to java world string contains 21 characters |