Given (we are reading it by the user) a string and we have to find its length using Java program.
To get the length of the string, we are using length() method of string class.
import java.util.Scanner;
public class ReturnLength
{
public static void main(String[] args)
{
// create object of string and scanner class.
String text;
Scanner SC=new Scanner(System.in);
// enter the string here.
System.out.print("Enter the string : ");
text=SC.nextLine();
// find string length
int length = text.length();
// print length.
System.out.println("Length of the given string " + text+ " is : " +length );
}
}
First run:
Enter the string : Include Help
Length of the given string Include Help is : 12
Second run:
Enter the string : We are programmers.
Length of the given string We are programmers. is : 19
Given (we are reading it by the user) a string and we have to find its length using Java program.
To get the length of the string, we are using length() method of string class.