Using CharAt method in Java.,

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganapathidev
    New Member
    • Dec 2007
    • 6

    #1

    Using CharAt method in Java.,

    Hi,
    iam Ganapathi .,

    can any one explain me where i have don mistake in this code........,
    class I {
    StringBuffer sb = new StringBuffer("T he boy is going ");
    String ind() {
    for ( int i=0 ;i<sb.length() ; i++) {
    System.out.prin tln("index of " + i + " " + sb.charAt(i));
    }
    return sb.charAt(i);
    }
    }

    class TheBoy {
    public static void main(String []args) {
    I s1 = new I();
    String v = s1.ind();
    System.out.prin tln(v);
    }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by ganapathidev
    Hi,
    iam Ganapathi .,

    can any one explain me where i have don mistake in this code........,
    class I {
    StringBuffer sb = new StringBuffer("T he boy is going ");
    String ind() {
    for ( int i=0 ;i<sb.length() ; i++) {
    System.out.prin tln("index of " + i + " " + sb.charAt(i));
    }
    return sb.charAt(i);
    }
    }

    class TheBoy {
    public static void main(String []args) {
    I s1 = new I();
    String v = s1.ind();
    System.out.prin tln(v);
    }
    }
    I see two mistakes:
    1. Method ind is defined with return type String, but you attempt to return a char. Which is correct?
    2. Variable i is defined in the for loop, so it is not available after it, so expression "return sb.charAt(i)" is illegal. Again, I think you need to reconsider what should be returned. I notice you haven't told us what you are trying to do, what your goal is.

    Comment

    Working...