Results 1 to 8 of 8
Thread: Help with charAt()
- 03-21-2010, 03:56 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
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!
- 03-21-2010, 04:02 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 03-21-2010, 04:08 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
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
- 03-21-2010, 04:10 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
Nevermind, turns out the space bar has an ascii value.
-
Java Code:int lengthWithoutSpaces = myString.removeAll(" ").length();
- 03-21-2010, 04:17 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
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....
- 03-21-2010, 04:19 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
And also, I'm sorry but I'm a n00b, how can I make my .java file self executable?
- 03-21-2010, 05:27 PM #8
Similar Threads
-
using String.charAt() for semicolons
By porchrat in forum New To JavaReplies: 5Last Post: 01-11-2010, 01:40 PM -
Palindrom - method charAt()
By user in forum New To JavaReplies: 10Last Post: 11-16-2008, 04:37 AM -
Help With Input.charAt(LastIndex);
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks