jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Plz help me, out, I am a newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Plz help me, out, I am a newbie" Watch "Plz help me, out, I am a newbie" New topic
Author

Plz help me, out, I am a newbie

Tirthankar Mukherjee
Ranch Hand

Joined: Apr 08, 2006
Posts: 51
I was trying to program, a simple sritng reversing one, but I got stuck, plz help me out, I will be really grateful


import java.io.*;
class Reverse
{
public static void main(String args[])
throws Exception
{
BufferedReader tir = new BufferedReader(new InputStreamReader(System.in));
String str;
//char c[];
int count=0;
str=tir.readLine();
char c[]=str.toCharArray();
for(int i=0; (c[i] != '\n');i++)
count=i;
for(int j=count;j>=0;j--)
System.out.println(c[j]);
}
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

What is your question? this looks like a homework assignment, and we generally don't just do it for you.

What exactly, is the code doing different than what your expect? or where are you stuck?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Error messages can be very helpful...

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
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
Hey Dude..

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.....
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
i think he is trying to learn java (loops, arrays, ...) and thats why he is doing it the "hard way".

since this is not production code it is OK to "re-invent the wheel". otherwise you will not learn the basics...


p
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
Yes Pascal , You are definitely right !
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
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();
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"AmitB.,"

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.

You can edit your name here.

Thank you for your prompt attention!

-Marc
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

so, can you tell us exactly where you are stuck? let's start here:

is your code compiling?
Tirthankar Mukherjee
Ranch Hand

Joined: Apr 08, 2006
Posts: 51
Originally posted by Tirthankar Mukherjee:
I was trying to program, a simple sritng reversing one, but I got stuck, plz help me out, I will be really grateful


import java.io.*;
class Reverse
{
public static void main(String args[])
throws Exception
{
BufferedReader tir = new BufferedReader(new InputStreamReader(System.in));
String str;
//char c[];
int count=0;
str=tir.readLine();
char c[]=str.toCharArray();
for(int i=0; (c[i] != '\n');i++)
count=i;
for(int j=count;j>=0;j--)
System.out.println(c[j]);
}
}
Tirthankar Mukherjee
Ranch Hand

Joined: Apr 08, 2006
Posts: 51
Originally posted by Tirthankar Mukherjee:
[QB][/QB]


my prog. is compiling but giving an error :
Error in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Reverse.main(Reverse.java 13)

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 ??
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Let's settle this down once for all. If this is for a College homework this ain't going to help. This is the answer.

There are a few ways to do it. Here you can see two of them.

This is the one your are pursuing:



This is a simpler one:



Good luck!

PS
If this is for a College homework, believe me you're going to need it.
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
Tirthankar Mukherjee
Ranch Hand

Joined: Apr 08, 2006
Posts: 51
Originally posted by Edwin Dalorzo:
Let's settle this down once for all. If this is for a College homework this ain't going to help. This is the answer.

There are a few ways to do it. Here you can see two of them.

This is the one your are pursuing:



This is a simpler one:



Good luck!

PS
If this is for a College homework, believe me you're going to need it.

[ April 15, 2006: Message edited by: Edwin Dalorzo ]


I did not understood your code, this is not a college homework, I am doing it myself, getting prepared advanced for the next Sem.I am REALLY a newbie, do u get me ???javascript: x()
banghead
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Ok, ok, buddy. I want to help. Tell me, what is it you do not understand so that I can hel you?

Come on! Give me a chance to give you a hand!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
This is an old array, its size is 7, because it has seven character, but as it is zero-based it starts at 0 and finishes at 6.



The newArray index is controlled by the i variable and it increments every loop (i++), while the oldArray is controlled by the j variable and it decrements in every loop (j--)

Do you get it now, pal?
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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[5] 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[0] is 'j'
c[1] is 'e'
c[2] is 't'
c[3] is 't'
c[4] is 'a'
c[5] 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 ]
Tirthankar Mukherjee
Ranch Hand

Joined: Apr 08, 2006
Posts: 51
Thanx a lot the problem is solved !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Plz help me, out, I am a newbie
 
Similar Threads
String Sorting error
Part of program not printing output
Problem in file transfer
Can Someone tell me whats wrong with this RLE simple code???
implementing the hash map