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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.