Unable to get the idea

• 11-20-2011, 10:17 AM
gozuhair
Unable to get the idea
Hi All

I am new in Java and reading one of the java book and stuck on one program, the program is below

Code:

```Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (where 1 means awful and 10 means excellent). Place the 40 responses in an integer array, and summarize the results of the poll.```
Code:

``` // Fig. 7.8: StudentPoll.java 2      // Poll analysis program. 3         public class StudentPoll 5      { 6        public static void main( String args[] ) 7        { 8        // array of survey responses 9        int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10    10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 11      4, 8, 6, 8, 10 }; 12      int frequency[] = new int[ 11 ]; // array of frequency counters 13 14      // for each answer, select responses element and use that value 15    // as frequency index to determine element to increment 16    for ( int answer = 0; answer < responses.length; answer++ ) 17    ++frequency[ responses[ answer ] ]; 18 19    System.out.printf( "%s%10s", "Rating", "Frequency" ); 20 21    // output each array element's value 22    for ( int rating = 1; rating < frequency.length; rating++ ) 23    System.out.printf( "%d%10d", rating, frequency[ rating ] ); 24    } // end main 25    } // end class StudentPoll```
Can somebody explain me the line no 17. Thank you in advance
• 11-20-2011, 05:13 PM
Norm
Re: Unable to get the idea
Quote:

You need to read the tutorial or your text about how for loops work. This is fairly standard in many computer languages.
Go to this site and Find "for statement" Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)
• 11-20-2011, 05:28 PM
gozuhair
Re: Unable to get the idea
Thanks for the reply but i am not talking abount loop statement, I am talking abount the below code after loop statement.

Quote:

• 11-20-2011, 05:28 PM
Fubarable
Re: Unable to get the idea
His issue is with this statement:
Code:

`++frequency[ responses[ answer ] ];`
My suggestion is that you split this up into several lines to better understand it.

Code:

```int thisAnswer = responses[answer]; ++frequency[thisAnswer];```
Note that

Code:

`++frequency[thisAnswer];`
is the same as

Code:

`frequency[thisAnswer] = frequency[thisAnswer] + 1;`
• 11-20-2011, 05:30 PM
Norm
Re: Unable to get the idea
Dyslexia, or need new glasses.
• 11-20-2011, 05:35 PM
gozuhair
Re: Unable to get the idea
Thanks for the reply, could you please explain it in more details, actually, I am always afraid from array topic and this time I dont want to compromise it.
If you can provide me an example then I will really appreciate you.
• 11-20-2011, 05:38 PM
Fubarable
Re: Unable to get the idea
Consider using a debugger such as one that is available from NetBeans or Eclipse (or likely whatever IDE you're using) and step through the code to see what happens to the frequency array at that line as this can tell you a lot.
• 11-20-2011, 05:40 PM
gozuhair
Re: Unable to get the idea
I am using Eclipse but i dont know how debugger works, will try to see the values in debugger and then let you the exact situation. Thank you very much