C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs » Java basic programs

How to get length of the string in java?

In this java program, we are going to take string as an input and get the length of the string, to read a string as an input, we are using ‘nextLine()’ method of ‘string’ class.
Submitted by IncludeHelp, on November 24, 2017

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 : ");

		// 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

You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions