-2
String str="This is interesting";

How can I find the length of the string ?

Conditions:

1) Not to use any String methods

16
  • 4
    Count with your fingers?
    – Mat
    Mar 10, 2012 at 14:50
  • 4
    Maybe it's me, but I don't see any educational value in such questions
    – M Platvoet
    Mar 10, 2012 at 14:52
  • Write a loop that counts the letters.
    – alexis
    Mar 10, 2012 at 14:55
  • 1
    possible duplicate of String length without using length() method in java. Also read this... Its interesting... Mar 10, 2012 at 14:58
  • 2
    StringBuilder sb = new StringBuilder(string); return sb.length(); Mar 10, 2012 at 17:08

2 Answers 2

2

If you can't use any String methods to find the length of the String, you can use reflection (Hint: String#length returns the field count). However, seeing as this is homework, I think you may have misunderstood the question you're being asked.

2
  • I haven't been understanding it differently. I am meant not to use any methods in String. They want us to think differently and find out. Using reflections ?? How?
    – Anuj Balan
    Mar 10, 2012 at 14:51
  • @Ajj I linked you to the reflection tutorial. This is homework, so I won't do it for you. You probably want to take a look at the Member section of that tutorial.
    – Jeffrey
    Mar 10, 2012 at 14:53
0

is str.length(); still not allowed?

OR New Link (StringLength.java)

2
  • 3
    Bad site. Invalid security certificate.
    – Hot Licks
    Mar 10, 2012 at 14:59
  • 1
    ok due to bad site comment, i'll update my answer.
    – John Woo
    Mar 10, 2012 at 15:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.