File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Nested for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested for loop" Watch "Nested for loop" New topic
Author

Nested for loop

Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
Ok, dumb question day. I'm writing a Java test and I'm having a little trouble with the nested for loop. y initializes inside the loop, but outside it is no longer initialized so there is a compile error at the println. How do I get y out of the loop? Also is there a cleaner way to set up an array?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Check this code. Lemme know if you need more

[ August 25, 2003: Message edited by: Pradeep Bhat ]

Groovy
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Rick -
The compiler's not saying that y wasn't intitialized, it's saying it may not have been initialized. By making a minor change, you make sure that y is properly initialized, and that makes the compiler a happy camper.
If you change line 6 to initialize the variable y -- or if you make these into instance variables (which are automatically intitialized) and place them inside a class that you instantiate -- the code compiles fine. Here's how I would make the first change:

Hope this helps!
Regards,
Jeff


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376

Let's see here. Okay, first thing is a pure style issue. I never start a variable name with a capital letter - that syntax is reserved for class names. So I'm going to call the variable myTable. But that little nitpick aside, let's do two things. First, array initialization. I love this stuff. Try this on for size:

Isn't that a little easier? Sure is for me. Okay, now on to the code. I'm not sure what you even need the variable "z" for; you have your indices in x and y just fine. So try this:

Notice that I added the curly braces, even though they were not strictly necessary. This adds readability and also makes it less likely to make mistakes in thinking what is inside the loop and what is not.
Also, I used a lot of extra spaces. There's really no reason not to. The compiler removes white space, so feel free to use it liberally in you source code. It's easier on the eye.
Joe
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
I don't want to print inside the for loop, I want to print once out side of it after running the loop. I'm rewriting a test question from C++ to Java and the almight Sun Developers have decided to make for loops difficult by creating new instances of the variable inside of the for loop that exist in their own little world. Initializing y=0 before the loop will keep y as 0 after the loop. What I really need is a way to get a variable out of the for loop.
Thanks for the better initialization Joe, that helps.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Rick -
I think you're misunderstanding the compiler error message.
The compiler is not making a new instance of the local variable y. What it's saying is that since a for loop can potentially have zero iterations if the test condition is met before the loop runs, that in such a case the variables declared in a method and initialized in the for loops may not be initialized. Doesn't mean they aren't, just that they may not be, and the compiler properly won't permit that. One of the jobs of the compiler is to catch potential problems before you release your code. If the compiler permitted uninitialized variables in running code, you would potentially have a lot of runtime exceptions and a lot of unhappy customers or users.
There's another option, that I mentioned above: If you put your code into a class and have the three counters (x, y, z) as instance variables, then instantiate the class in a main() method, now they would be initialized to the default value of 0 and the compiler would be happy.
I hope this helps clear the confusion.
Regards,
Jeff
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Originally posted by Rick Gentry:
I don't want to print inside the for loop, I want to print once out side of it after running the loop.

Okay, now I'm a little confused. I'm going to ask a standard question that I ask all the time: what's the business problem being addressed? I always ask that in order to get a word description of the problem. In this case, you're doing a little "learning exercise" so perhaps the phrase "business problem" is a incorrect. Let's try instead: what exactly are you trying to print outside of the loop? The last element? I guess I don't understand what the purpose of the loop is.
Joe
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
Yes, the loop is supposed to get to the last element in the array and print that once. y is never initialized outside of the for loop though and now I'm back to my original question.....How do you get a variable out of a for loop?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
You don't have to get a variable out of a for loop. If you have declared and initialized a variable outside of the for loop, and it's visible in the for loop, then you already have access to it assuming you didn't call return or System.exit() in the loop.
Have you compiled and tested your code snipped with the change I suggested above? I did, and I had no problem correctly running the code.
Regards,
Jeff
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
Thank you Jeff, strangly enough that works. I feel dumb now.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
You're welcome, Rick.
Don't feel dumb - Java has a pretty big learning curve, and you just never know when a concept will click right away or fight you for awhile.
For example, I came from C, which I still love and use, and I had the hardest time understanding why I couldn't access a non-static variable from my main() method. In C, you do that all the time! One day it clicked for me, and I felt dumb for awhile, but I now realize that there's so much to learn in computer science that there's no way we can know it all, and some things just take time.
I've learned to have patience with myself and I've turned learning programming into a puzzle-solving game: Some puzzles I solve quickly, some take a long time, some I still don't get. But at least I'm having fun with it again.
Regards,
Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested for loop
 
Similar Threads
dan chrisholm's mock exam 1
|| operand
nesting loops
scjp chapter 5 q...5 page no 303
how to compare the values in arrays