Hi Murali
You are right. I was thinking at an array of Strings.
It may work either:
||
*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();
char[ ] sChars = s.toCharArray();
int len = 0;
for ( char element : sChar ) len++; // <-- counting the elements
System.out.println(len);
}
*catch*(Exception e){}
}
}
or using the "cheating" tip from vivek hari (who is smarter):
||
*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();
int len = 0;
try {
while ( true ) char c = s.charAt(len++); // indefinitely get
the next character
// provoking a crash when the character after the last one is read
} catch ( IndexOutOfBoundsException oobx ) { }
System.out.println(len);
}
*catch*(Exception e){}
}
}
Hope it helps
Mihai
Le 23/01/2011 18:30, MURALI Sp a écrit :
Hi,
But we cannot apply this method...
For each loop is only applicable for arrays and collection. String is
not an array and it is not a collection.
On Sun, Jan 23, 2011 at 3:46 PM, Mihai DINCA <[email protected]
<mailto:[email protected]>> wrote:
Hi Parveen
This is tricky, as the String is supposed to be an object, so a
sort of "black box" showing only its public atributes and methods.
So, you are not supposed to be able to "look inside the String" to
get the length other than through the "length()" method.
There is an workaround, but it is cheeting. However, try this:
||
*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();
int len = 0;
for ( String element : s ) len++; // <-- counting the elements
System.out.println(len);
}
*catch*(Exception e){}
}
}
Le 22/01/2011 19:59, Parveen Thakur a écrit :
Hi all,
How we can check the length of string without
use of Lenght() menthod
|*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){}
}
}|
--
Best Regards And Thanks :
Parveen Thakur,
Contact no.-9988208440.
--
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:[email protected]>
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:javaprogrammingwithpassion%[email protected]>
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en