Home Java Beginners length() Method In Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

length() Method In Java

Advertisement
In this section, you will learn how to use length() method of the String class.

length() Method In Java

     

In this section, you will learn how to use length() method of the String class. We are going to using length() method. These method are illustration as follow here:

Description of program:

Here, we will know that how many character of length in String. First of all, we have to define class named "StringLength". Inside of class we have to define method of class in  main class. After that we create one method which returns the integer type values. So we are using to length() method of the String class for the purpose.

length() method : This method return the length of this string as an integer value.

Here is the the code of this program:

import java.io.*;

class StringLength{
  public static void main(String[] args){
  try{
  BufferedReader object=
 
new BufferedReader (new InputStreamReader(System.in));
  System.out.println("Eneter string value:");
  String s=object.readLine();
  int len=s.length();
  System.out.println(len);
  }
  catch(Exception e){}
  }
}

Output this program:

C:\java_work>javac StringLength.java

C:\java_work>java StringLength
Eneter string value:
amar
4
C:\java_work>

Download this program:

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Ask Questions?    Discuss: length() Method In Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
joexce
January 1, 2012
function..

i have problem with java function,like length() i don't know how much function in java can you tell me about it ?
joexce
January 1, 2012
function

i have problem with function in java, i dont know how much function like length() etc can you tell me about it? i need reference from java,please
ceasar
February 24, 2012
how i can get the length in this input

import java.io.*; /** * Write a description of class ewanq here. * * @author (your name) * @version (a version number or a date) */ public class input1 { public static void main (String [] args)throws Exception { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); clear_string c = new clear_string(); c.clear_string(); int n; String strnum1; System.out.print("Enter value"); strnum1= input.readLine(); n = Integer.parseInt(strnum1); System.out.print( n + " "); while (n!=1) { if (n%2==1) { n=(3*n)+1; System.out.print(n + " "); } else { n =n/2; System.out.print(n + " "); } } } } how can i get the length of this input..thanks for the fast responds
sachin sharma
March 16, 2012
java

How to run package
Art Bergquist
November 27, 2013
Typo - "Eneter" need to be replaced with "Enter"

Both occurrences "Eneter" need to be replaced with "Enter" above (on http://www.roseindia.net/java/beginners/length.shtml).
DMCA.com