This tells you the program is trying to access an array index that's out of bounds. In particular, the out of bounds index is 4. (Note that the String I entered above was "test".) The error message also tells you that this happens on line 13 of your code.
That should suggest what's not working.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
This is such a easy problem and you got scared ?? Always try to debug your code... I've seen that you've been using Exception in your class but there is no need for that.. I mean its just an small code.. Also , There is ready methods to reverse Strings I dont know why are you going for this approach of scanning names and then reversing.. Anywayz here's the code which reverses the name. Hope this helps..
Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
The other members are right, if you are trying to learn something, you should try it from all angles yourself, however there I believe that the exception youre getting about the array index bieng out of bounds could cause you to tinker around as much as you want with the looping constructs till your blue in the face and red in the eyes and still not get anywhere. the problem is not really youre array being out of bounds (thats just the the effect) of something, the cause if something else. youre asking youre loop to run till it encounters a "\n". but have u added a "\n" at the end of the string? no. youre code would work if you go: str=str+'\n' right after you go str=tir.readLine();
Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.
Basically what I was trying is to convert the string accepted to a char array and then trace the end of line with '\0' or '\n' as I did in C, then reverse the array, but I dont know that the string accepted is in array form or some others, in C I used to accept in the form of array and then reverse it, but I dont know how to achive it , what I need to do now ??
Originally posted by Tirthankar Mukherjee: ...my prog. is compiling but giving an error : Error in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Reverse.main(Reverse.java 13)...
Let's take a step back, because you need to understand how to use error messages to debug your code. As I posted yesterday, this message tells you the program is trying to access an array index that's out of bounds. In particular, the out of bounds index is 5, and it happens on line 13 of your code.
So look at line 13, and ask why an index of 5 is out of bounds...
for(int i=0; (c[ i ] != '\n'); i++)
Here, "c" is a char array made up of the chars in the String you entered. If c is out of bounds, that means you entered a String of only 5 chars. Let's say for example that you entered "jetta". Remember, array indexing starts at zero, so...
c is 'j' c is 'e' c is 't' c is 't' c is 'a' c is Out Of Bounds!
So how did it reach this out of bounds index? Look back at your test condition on line 13...
(c[ i ] != '\n')
The loop will continue until the indexed char is '\n'. But as this code demonstrates, the length of the array is equal only to the number of chars in the String you entered. In other words, it does not include a '\n' char at the end.
As pointed out in a previous post, the problem is you assumed that the String returned by readLine() would terminate with a new line character, and this assumption led to the error. Checking the API docs for BufferedReader tells you that readLine() returns "A String containing the contents of the line, not including any line-termination characters...."
In general, iterating through an array should be based on the known size of the array -- not what you think is in it. The irony is that the sole purpose of this loop seems to be for determining the size of the array, so that you can use this value in the following loop. But arrays already have a member called "length," so you can get what you want with c.length. The first loop is simply not necessary, and your second loop could use...
for(int j = (c.length - 1); j>=0 ; j--) [ April 15, 2006: Message edited by: marc weber ]