Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

for loop is not entered?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 380
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a very basic and trivial question but anyway my for loop is not been entered I used a sysout to see if I enter the loop and it looks like I don't

anyway I checked to see how big the length is,when I type in a name bob the length is 3,

so I set i = the length which in our case is 3,then I check if i is equal to zero then I run the loop then I decrement it and run until it hits zero,

yet the loop for some reason never enters,

where abouts is my logic wrong?

thanks



 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
where abouts is my logic wrong?


Well, the loop is entered when "i" is equal to zero. And the "i" variable starts off as the length of the string.

Is the length of the string zero? That is the only way the loop will run.

Henry
 
Pete Letkeman
Ranch Hand
Posts: 184
8
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is the ==
This line

should be this
 
Adam Chalkley
Ranch Hand
Posts: 380
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code a little(still not too sure why the original code wouldn't work)


but now I get an arrayoutofbounds exception,I thought maybe the exception might occur when i = 0  and I check for i - 1 but I added a if statement to check for that and yet to my surprise I still get an an arrayOutfOfBounds exception,


 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:Problem is the ==
This line

should be this


and to add a bit to that, if I'm right also the line will cause a problem since you enter the for loop with the length of the string, but if you add the length to the charAt() method it will cause an out of bounds exception.
 
Pete Letkeman
Ranch Hand
Posts: 184
8
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The length of a string could be 3, but the string is internally treated as char array, so the array only has elements at 0, 1, 2. You are getting the out of bounds error because you are trying to access an the string at element 4. If you changed your int length = a.length(); to int length = a.length()-1; then you will not have array out of bounds problems.

In theory you could run into null pointer issues if you did not check to see if the string object was initialized to something other then null.
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a Last and Before variable in your loop.

But if you are at i=0, the before variable will try lookup charAt(0 -1) which gives an exception.
You could solve this probably by changing your change you for loop condition from "i > = 0" to " i > 0"
 
Pete Letkeman
Ranch Hand
Posts: 184
8
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, looks like you are correct. Sorry about that.
 
Adam Chalkley
Ranch Hand
Posts: 380
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true it should be length -1,

but I'm still having a problem it's not appending the letters to the string


 
Adam Chalkley
Ranch Hand
Posts: 380
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok guys I cleaned up the solution

no need to even swap the variables,anyway I'm still not sure why the chars weren't getting appended in my example above this one,

 
Adam Chalkley
Ranch Hand
Posts: 380
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw thanks for all the help =)
 
Pete Letkeman
Ranch Hand
Posts: 184
8
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the fault



Each time you start the loop you are creating a new StringBuilder object, overwriting what was there before.

Comment or remove that line and you are good to go.
 
Pete Letkeman
Ranch Hand
Posts: 184
8
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as helping. I like to think that now only do I learn when I help, but the end user learns as well. I would also like to think that other people would be kind enough to help me out (and they have) when I have problems for please pay it forward when you can.
 
Adam Chalkley
Ranch Hand
Posts: 380
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks  Pete I don't know how I made that mistake,

and true I find that looking at peoples code and trying to solve where they went wrong is a huge help also

that's what makes this community great
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!