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]> 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] > 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 > > -- > To post to this group, send email to > [email protected] > To unsubscribe from this group, send email to > [email protected]<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
