|
-
February 1st, 2005, 12:32 PM
#1
for loops
I am very new to this and would like a little help. I understand that the for loop has a declaration, a test and an incrementer. What I am trying to do below is print a entered string in reverse.
I keep getting an out of bounds exception because I am reaching 0, the next step being -1.
I just can't seem to work this out. Could someone help please?
This is my code.
for (i=length-1;i<=length;i--)
{
mychar=wordString.charAt(i);
System.out.println(""+mychar);
System.out.println (""+i);
}
My apologies if this is a stupid question
Cheers
Harlequeen
-
February 1st, 2005, 01:44 PM
#2
Re: for loops
Should be:
for (i=length-1; i>=0; i--)
{
mychar=wordString.charAt(i);
System.out.println(""+mychar);
System.out.println (""+i);
}
You are starting at the back of the string, and decrementing i, so the test is that the loop should continue while i is greater than or equal to zero.
HTH,
Matt
-
February 1st, 2005, 07:28 PM
#3
Re: for loops
This is surely another example where 30 seconds stepping through the code with pencil and paper would spot the error.
You don't need a keyboard and screen to write or debug code. Quite often, they are a positive distraction...
A pencil and paper are the best programming tools out there...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 2nd, 2005, 09:01 PM
#4
Re: for loops
StringBuffer.reverse()
-
February 2nd, 2005, 09:22 PM
#5
Re: for loops
 Originally Posted by harlequeen
I am very new to this and would like a little help. I understand that the for loop has a declaration, a test and an incrementer. What I am trying to do below is print a entered string in reverse.
I keep getting an out of bounds exception because I am reaching 0, the next step being -1.
I just can't seem to work this out. Could someone help please?
This is my code.
for (i=length-1;i<=length;i--)
{
mychar=wordString.charAt(i);
System.out.println(""+mychar);
System.out.println (""+i);
}
My apologies if this is a stupid question
Cheers
Harlequeen
Which is not surprising. When you write "i<=length", you're giving your loop an upper bound (which is completely unnecessary in this particular instance), but not a lower bound. A lower bound is important here since you're decrementing here from a set point to zero.
You're getting this particular error, because there is no lower bound and as a result you're going into the negatives (and there is no way that an array can have a negative position.) Instead of "i<=length", try "i >= 0".
-
February 3rd, 2005, 04:08 AM
#6
Re: for loops
@dlorde:
A pencil and paper are the best programming tools out there...
i like the way you approach erroneous code, and i think this way in the most problem cases, but there are few failures, where i wont miss my debugger
Peter
_______________________
Homer: Okay, brain. You don't like me, and I don't like you, but let's get through this thing and then I can continue killing you with beer.
Homer's Brain: It's a deal!
-
February 3rd, 2005, 05:39 AM
#7
Re: for loops
i'd also like to note that:
i <= length
shouldnt be used for upper-bounding an array, because an array of length 10, actually has elements 0 to 9. if youre testing for lessthan or EQUAL to 10, then at some point, you will attempt to access array[10], which is out of bounds
standard loops for iterating an array forwards and reverse:
for(int i=0; i<array.length; i++)
for(int i=array.length-1; i>=0; i--)
-
February 3rd, 2005, 05:15 PM
#8
Re: for loops
 Originally Posted by universer
but there are few failures, where i wont miss my debugger 
I must admit, it's a partly case of do as I say, not as I do
I use the debugger constantly - but I do always have a pencil and paper to hand, because I believe it's hard to use a debugger effectively without them. With experience, many of the simple bugs like off-by-one, it's-null-but-it-shouldn't-be, == not =, and == not equals() errors happen less often, and are easier to spot when they do, so student programmers have even more to gain more from p&p than experienced programmers - it took me some time to realise this when I was a student, so I try to pass it on.
Necessity is the mother of invention. Laziness is the mother of necessity...
Anon Perl Hacker?
So it follows: the grandmother of invention is laziness...
Donald Noyes
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 3rd, 2005, 08:22 PM
#9
Re: for loops
 Originally Posted by dlorde
I must admit, it's a partly case of do as I say, not as I do
I use the debugger constantly - but I do always have a pencil and paper to hand, because I believe it's hard to use a debugger effectively without them. With experience, many of the simple bugs like off-by-one, it's-null-but-it-shouldn't-be, == not =, and == not equals() errors happen less often, and are easier to spot when they do, so student programmers have even more to gain more from p&p than experienced programmers - it took me some time to realise this when I was a student, so I try to pass it on.
That's actually quite true, especially when you're messing around with pointers.
 Originally Posted by dlorde
Necessity is the mother of invention. Laziness is the mother of necessity...
Anon Perl Hacker?
So it follows: the grandmother of invention is laziness...
Donald Noyes
rofl Nice .
-
February 5th, 2005, 12:15 PM
#10
Re: for loops
thanks for your help. I did, in fact, find the answer myself when I gave myself a break and then went back. As I said, I'm very new to this, so failing to get something right is not stranger to me at the moment.
I do try and use pen and paper to follow the progression of a program, but I got bogged down.
Anyway, thanks for your help and your comments, I am finding out that trial and error is a great learning tool
Cheers
Harlequeen
-
February 5th, 2005, 12:36 PM
#11
Re: for loops
Some hints (or cookie recipes):
To generate the sequence 0, 1, 2, ... n-1:
for (int i = 0; i < n; ++i) {
}
To generate the sequence 1, 2, ... n:
for (int i = 1; i <= n; ++i) {
}
To generate the sequence n-1, n-2, .... 2, 1, 0:
for (int i = n - 1; i >= 0; --i) {
}
To generate the sequence n, n-1, .... 2, 1:
for (int i = n; i >= 1; --i) {
}
To generate the sequence 1, 3, 5, 7, ... 99:
for (int i = 1; i < 100; i += 2) {
}
To generate the sequence 0, 2, 4, 6, 8, .... 100:
for (int i = 0; i <= 100; i += 2) {
}
If you understand and memorize some of those patterns above, you'll never have problems with "for loops" biting you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|