Results 1 to 8 of 8
  1. #1
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Unhappy Help with charAt()

    Hi everyone, I have looked and really need help with my specific problem.
    My code is as follows:

    int counter;
    for (counter = 0; counter < phrase.length(); counter++);
    {
    int p = counter;
    int shiftNum = Integer.parseInt(shift.trim());


    System.out.print("\n" + phrase.charAt(p)+ shiftNum);
    }

    My variables are all accounted for and there are no errors in the code, but i keep getting this error message:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(Unknown Source)
    at Crypto.main(Crypto.java:49)

    Thanks in advance!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by HackerOfDoom View Post
    Hi everyone, I have looked and really need help with my specific problem.
    My code is as follows:

    for (counter = 0; counter < phrase.length(); counter++);
    What is that semicolon doing there at the end of the line? Get rid of it.

    kind regards,

    Jos

  3. #3
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Thanks for your reply, It totally worked (and I deleted the "\n" lol, because it was printing on separate lines). Another question I have about this project is that I ask the user to enter input without spaces, is there any way for the .length() command to omit spaces in its calculation?
    Thanks again

  4. #4
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Nevermind, turns out the space bar has an ascii value.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Ok I'll make sure to use the tags next time. Also, since my real point in this program is encryption/decryption for a school thing, I decided to leave in the spaces because they have an ascii value after all, so we can add numbers to the ascii value, then translate it back with (char). I got the idea from hackthissite.org, because they do something with ascii values that I am just realizing I could have used a program to solve. Anyway, thanks a bunch....

  7. #7
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    And also, I'm sorry but I'm a n00b, how can I make my .java file self executable?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by HackerOfDoom View Post
    And also, I'm sorry but I'm a n00b, how can I make my .java file self executable?
    Trail: Deployment (The Java™ Tutorials)

    db

Similar Threads

  1. using String.charAt() for semicolons
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 01-11-2010, 01:40 PM
  2. Palindrom - method charAt()
    By user in forum New To Java
    Replies: 10
    Last Post: 11-16-2008, 04:37 AM
  3. Help With Input.charAt(LastIndex);
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:22 AM

Posting Permissions

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