• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I return an integer in a for loop?  RSS feed

 
David Miser
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, HELLO! This is my first post here and it seems like a great place. Also, sorry for the long post, haha.

After putting it off for a long time I finally buckled down and started learning Java. I have been following the excellent tutorials on ProgramByDoing.
I am currently on the 47'th assignment.

I finished it fairly easily, but sometimes I like to add little things just so I can further how much I'm able to learn. This time I wanted to add a way to "catch" if the user were to enter a number beside 1, 2, or 3. Normally I do this by having a for loop start right before the user enters a number( loop = 0; loop != 0;). it would then run through an if/else statement to check the entry. If it were correct it would would add 1 to 'loop' and the for loop would exit. If the entry isn't what I want, then it would simply return to part where the user enters a number.

However this causes a problem. If the loop starts before the user input, any integer the Scanner gets is in the loop, and that confuses java if I try to use that integer outside the loop, further down (saying the variable may not have been initialized). I would solve this by simply stating that x = 0 before the loop starts, then it would be changed once things go through the loop check.

This has never caused me problems until now. Now my code seems to simply take that zero and run it through to the end ( the for loop doesn't change it).

I have no experience with 'return'. Is it possible to use return to get that integer outside the loop ?
Here is my current code (which doesn't work, as described above).




THANKS FOR ANY HELP!
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing you could do is change it to for(loop = 0; loop == 0) However, there is a much simpler solution to this.

you should only use a for loop and an integer variable if your loop had a limit to how many times you wanted it to run. In this case, you want your loop to iterate an infinite amount of times (so long as the input is invalid). Since you want a loop that could potentially run forever, you would use while (true) {}. But now you need a way to exit the loop if the input is valid. The keyword for that is break. So if you get to the else block, then the input is valid. So adding break; there would work.

Also, a number could never be less than one AND greater than three. So your && (meaning and) should be changed to || (meaning or).

I almost forgot: welcome to the ranch!
 
David Miser
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works! Thank you
Also, I don't know how I missed that &&, haha. While loops are the next segments of the course, so no wonder I didn't know about them. Is there a way to tag this post an 'Answered'?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Miser wrote:That works! Thank you
Also, I don't know how I missed that &&, haha. While loops are the next segments of the course, so no wonder I didn't know about them. Is there a way to tag this post an 'Answered'?


Your welcome! I'm surprised while loops weren't covered in the first 47 lessons. lol. I don't believe there is a way to mark it as Answered. But seriously, kudos to you for self-teaching and keep it up; it's such a mentally rewarding thing.
 
David Miser
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:I'm surprised while loops weren't covered in the first 47 lessons.


They go at a very deliberate pace. He wants to make sure you understand why things work a certain way, as well. I'm very satisfied with them so far.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Miser wrote:First off, HELLO!...

Hello to you, and welcome to JavaRanch.

First off to you:

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time - see how much better it formats?. For future reference, please remember:
80 characters max.
and that includes string literals and comments.

Thanks.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!