File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes nested for loops, need more assignment help.... 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 loops, need more assignment help...." Watch "nested for loops, need more assignment help...." New topic

nested for loops, need more assignment help....

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
Okay, I have to write a program that displays the averages of four different sets of grades using a nested for loop. I wasn't quite positive whether or not I was doing it right, but I gave it a shot. Heres the code and the errors I keep getting...

Any help as to how I might improve this, or at least get it so that it runs would be most appreciated.
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hmmm, I think you need to do a bit of reading on what a for loop is and how you write one. A typical nested for loop can be seen in the code snippet below. Compile this code and run it - do you understand what is going on here?

Where are the scores you are averaging coming from? Are they input by the user?

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
I know what a nested for loop is, I'm just not too sure how to apply it to the problem. Its basically a for loop within a for loop. The scores are already given, so I have to somehow set them as doubles somewhere within the program.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
for (gr1=23.2; gr2=31.5; gr3=16.9; gr4=27.5; gr5=25.4; gr6=28.6)
Not sure what you are trying to accomplish here, but it's definitely not a nested "if". Look again at Michael's code, and think about the problem that you are trying to solve.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

a for-loop has a very specific structure. inside the parens there is an initialization, a test condition, and a 'what to do at the end of each loop'. each is separated by a semicolon - therefore, you should only have 2 semicolons. in your example, the compiler is confused because you have something like 5... it can't figure out what you are doing.
a for loop is often used to look at every element of an array...
so, maybe you could use a single for-loop to add all the values of one set of grades, if they were in an array. then after the loop, you could calculate the total and average.
but why do they say a nested loop? well, you actually have a set of (set of grades). this sounds like a 2D arrray. one loop (outer) would iterate over the set of sets, and the inner loop would iterate over the set of grades.
I'd try and just write the inner loop, over one set of grades, and the calucation first. once i was pretty sure i had that part working, and understood it, try and wrap that whole thing in another loop. this might then also require some tweaking of the inner loop, since the data would now be in a 2d instead of a 1d array.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I love this place.

Please ignore post, I have no idea what I am talking about.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

William: Now, now, now, Be Nice!
Fred is right in his description of the problem. You are attempting to write a for loop to calculate each set of grades. Instead, write one foor loop to calculate one set and then place that loop inside another which iterates over the individual sets.
It is possible to have more than one initializer in a for loop, but it is not recommended that you do so. Instead, put your grades into an array (or a List if you prefer) and then iterate over the array.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
Hi Brandi:
One way you can use nested loops to solve the problem at hand is:
1. create a 2-D array of 4 rows where each row holds a set of 4 grades
2. for each row, you can sum the contents of the elements of the row
3. so you need 2 loops, one for traversing the rows, and other for travesing the columns
public class MyClass
//1. declare 2-d array variable and initialise it with 4 values in each row
//int [][]myArray={....}
int sum=0;
public MyClass()
//2. here comes the ensted llop
for(int i=0; i<myArray.length;i++)
for(int j=0;j<4;j++)
{//sum the contents here
System.out.println("Sum of the"+ sum);
System.out.println("Average of set"+(sum/4));
public static void main(String []args)
{ MyClass mc=new MyClass();}

Hope that this helps
I agree. Here's the link:
subject: nested for loops, need more assignment help....
jQuery in Action, 3rd edition