Results 1 to 4 of 4

Thread: on string length

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    on string length

    if a string contains: "Hello" (without the inverted comas),what's the length?

    5? or is it 6 (is there a null value or something at the end)?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Nope, 5.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    thanks for your replies crptcblade. but i'm still in a rut...

    i'm using the string tokenizer to split up a string of characters, using the contents of another string (ID) as the delimiter:

    StringTokenizer st = new StringTokenizer(plaintext, ID, true);

    therefore, if the string is "HEL®LO", and that "®" is not in the ID string, the return of each token should be:

    H
    E
    L
    ®L
    O

    working with each token, i have to ignore characters that are not in the ID string.... thus i'm trying to get the last character in each token (delimiters being the values present in ID).... using:

    n = get.length();
    convget = get.charAt((n - 1));

    no problems in compilation but errors pop up during runtime:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
    ex out of range: 142
    at java.lang.String.charAt(Compiled Code)
    at Crypt.encrypt(Compiled Code)
    at CryptDriver.main(Compiled Code)

    arrrggghhhhhhhhhhhhhh!!!!!!!!!!!

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Using the code below i can't see how you would get a StringIndexOutOf BoundsException unless the String had a length of zero(which you would then get a negative index). Since the error you are getting is a positive value you are trying access an index greater than length - 1.
    Code:
    public class S{ 
     public static void main(String[] args){
      String s = "Hello"; 
      int l = s.length(); 
      char c = s.charAt((l - 1));
      System.out.println(c); 
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width