Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

basic java assignment question  RSS feed

 
Sean Jacobson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
hello,
I have been working on my java lab and cannot understand what I'm doing wrong.
I want the program to take an input of "piernine"
and out put "eirinnep". I have highlighted my problem in the code.
it says cannot find symbol variable (i), but i defined it in the for loop.
I have tried absolutely everything. My program is doing what i want
as replacing the (i)' s with 1's gives me an output of "ii"
any help would be greatly appreciated as I have spent way tooooo
much time on this and have no one too ask.



 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
On line 21 and 25, you don't need the semicolon right before the curly brace. For the first "for" loop you closed it before it did anything to "firststring".
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Report post to moderator
One problem is that you put an extra ; at then end of the for loop declaration, you need to remove those (on line 21 and 25).

Then, in the first for loop you open a code block at the end of line 21 and close it on line 22. That code block (marked by the {} brackets) is the code which would get executed in the for loop, and it marks the life of the variable i. If you want line 23to be executed in the loop it should be inside the brackets, and if not the variable i should be declared in a broader scope (declare it before the for loop so it has a larger scope than just the for loop.)

The second for loop may be a similar problem, but it looks like you are missing a closing bracket, which would mean the code won't compile. I am not sure where you intend the bracket to be, but as with the first loop, the variable you declare in the first part of the for loop on lives as long as the loop and then goes out of scope.
 
Sean Jacobson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
i seriously thought i messed up big, but
thats all it was, i was enclosing the for loop...i feel
dumb, thank you very much
 
Sean Jacobson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
Steve Luke wrote:One problem is that you put an extra ; at then end of the for loop declaration, you need to remove those (on line 21 and 25).

Then, in the first for loop you open a code block at the end of line 21 and close it on line 22. That code block (marked by the {} brackets) is the code which would get executed in the for loop, and it marks the life of the variable i. If you want line 23to be executed in the loop it should be inside the brackets, and if not the variable i should be declared in a broader scope (declare it before the for loop so it has a larger scope than just the for loop.)

The second for loop may be a similar problem, but it looks like you are missing a closing bracket, which would mean the code won't compile. I am not sure where you intend the bracket to be, but as with the first loop, the variable you declare in the first part of the for loop on lives as long as the loop and then goes out of scope.


yep, i got it running by removing the semicolons,
definitely going to be learning alot here thanks for
the responses
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Report post to moderator
Great, glad we could help, and welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Report post to moderator
Everybody makes that mistake some time.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!