This site requires JavaScript, please enable it in your browser!
Greenfoot back
biferony
biferony wrote ...

2015/5/5

Array comparison and Printing array problem

biferony biferony

2015/5/5

#
I am working on a project where I need to Print out two arrays and check what parts are the same. I tried using this code, but it didn't do anything.
1
2
3
4
if (Submitted_Answers[0] == Correct_Answers[0])
            {
                System.out.println("success");
            }
For the printing problem, I tired to just print out the (int) array, but the output was weird.
1
2
3
4
5
6
if (QuestionNumber == 20)
        {
            System.out.print (Submitted_Answers);
            System.out.print (Correct_Answers);
             
        }
danpost danpost

2015/5/6

#
What type of data do the array contain (how are the arrays declared)?
biferony biferony

2015/5/6

#
danpost wrote...
What type of data do the array contain (how are the arrays declared)?
They are integer arrays, they are declared like this:
1
2
int [] Correct_Answers = { 3, 2, 3, 2, 2, 1, 1, 3, 3, 2, 3, 1, 2, 3, 1, 1, 2, 3, 2, 3 };
int [] Submitted_Answers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
danpost danpost

2015/5/6

#
Okay. You have to compare each element in one array with its counterpart in the other array. You can use a 'for' loop and print out the value of the counter in each line of the print-out for easy referencing. Use the counter of the loop for the indecis of the arrays. You could even set up an int variable before the loop to count the number of correct (or wrong) submitted answers and print that out after the loop.
biferony biferony

2015/5/6

#
danpost wrote...
Okay. You have to compare each element in one array with its counterpart in the other array. You can use a 'for' loop and print out the value of the counter in each line of the print-out for easy referencing. Use the counter of the loop for the indecis of the arrays. You could even set up an int variable before the loop to count the number of correct (or wrong) submitted answers and print that out after the loop.
Can you give acctual code for these? I'm new to programming, so I don't know how to do all of those. If you want my full code, then you can see it below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
public class Clicker extends Actor
{
    int QuestionNumber = 1;
    int PercentCorrect = 0;
     
    int [] Correct_Answers = { 3, 2, 3, 2, 2, 1, 1, 3, 3, 2, 3, 1, 2, 3, 1, 1, 2, 3, 2, 3 };
    int [] Submitted_Answers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
     
         
     
    /**
     * Act - do whatever the Clicker wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Submitted_Answers[0] == Correct_Answers[0])
            {
                System.out.println("banana");
            }
        
       World Question1 = getWorld();
       GreenfootImage bg1 = new GreenfootImage("Question1.png");
       GreenfootImage bg2 = new GreenfootImage("Question2.png");
       GreenfootImage bg3 = new GreenfootImage("Question3.png");
       GreenfootImage bg4 = new GreenfootImage("Question4.png");
       GreenfootImage bg5 = new GreenfootImage("Question5.png");
       GreenfootImage bg6 = new GreenfootImage("Question6.png");
       GreenfootImage bg7 = new GreenfootImage("Question7.png");
       GreenfootImage bg8 = new GreenfootImage("Question8.png");
       GreenfootImage bg9 = new GreenfootImage("Question9.png");
       GreenfootImage bg10 = new GreenfootImage("Question10.png");
       GreenfootImage bg11 = new GreenfootImage("Question11.png");
       GreenfootImage bg12 = new GreenfootImage("Question12.png");
       GreenfootImage bg13 = new GreenfootImage("Question13.png");
       GreenfootImage bg14 = new GreenfootImage("Question14.png");
       GreenfootImage bg15 = new GreenfootImage("Question15.png");
       GreenfootImage bg16 = new GreenfootImage("Question16.png");
       if (QuestionNumber == 2)
        {
        Question1.setBackground(bg2);
        }
       if (QuestionNumber == 3)
        {
        Question1.setBackground(bg3);
        }
       if (QuestionNumber == 4)
        {
        Question1.setBackground(bg4);
        }
       if (QuestionNumber == 5)
        {
        Question1.setBackground(bg5);
        }
       if (QuestionNumber == 6)
        {
        Question1.setBackground(bg6);
        }
       if (QuestionNumber == 7)
        {
        Question1.setBackground(bg7);
        }
       if (QuestionNumber == 8)
        {
        Question1.setBackground(bg8);
        }
       if (QuestionNumber == 9)
        {
        Question1.setBackground(bg9);
        }
       if (QuestionNumber == 10)
        {
        Question1.setBackground(bg10);
        }if (QuestionNumber == 11)
        {
        Question1.setBackground(bg11);
        }
       if (QuestionNumber == 12)
        {
        Question1.setBackground(bg12);
        }
       if (QuestionNumber == 13)
        {
        Question1.setBackground(bg13);
        }
       if (QuestionNumber == 14)
        {
        Question1.setBackground(bg14);
        }
       if (QuestionNumber == 15)
        {
        Question1.setBackground(bg15);
        }
       if (QuestionNumber == 16)
        {
        Question1.setBackground(bg16);
        }
        
        
       if (QuestionNumber == 20)
        {
            System.out.println (Submitted_Answers);
            System.out.println (Correct_Answers);
             
        }
         
        //List QuestionCounter = getObjects(QuestionCounter.class);
        //if (QuestionCounter.size() == 2) addObject(new QuestionCounter(), 100, 100);
         
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (Greenfoot.mouseClicked(null))
        {
            if (mouse.getX() >= 198 &&
                mouse.getX() <= 366 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                //Greenfoot.setWorld(new question4());
                QuestionNumber += 1;
                //System.out.println( QuestionNumber );
                   if (QuestionNumber == 1)
                    {
                        Submitted_Answers [0] = 1;
                         
                    }
                   if (QuestionNumber == 2)
                    {
                        Submitted_Answers [1] = 1;
                    }
                   if (QuestionNumber == 3)
                    {
                        Submitted_Answers [2] = 1;
                    }
                   if (QuestionNumber == 4)
                    {
                        Submitted_Answers [3] = 1;
                    }
                   if (QuestionNumber == 5)
                    {
                        Submitted_Answers [4] = 1;
                    }
                   if (QuestionNumber == 6)
                    {
                        Submitted_Answers [5] = 1;
                    }
                   if (QuestionNumber == 7)
                    {
                        Submitted_Answers [6] = 1;
                    }
                   if (QuestionNumber == 8)
                    {
                        Submitted_Answers [7] = 1;
                    }
                   if (QuestionNumber == 9)
                    {
                        Submitted_Answers [8] = 1;
                    }
                   if (QuestionNumber == 10)
                    {
                        Submitted_Answers [9] = 1;
                    }
                   if (QuestionNumber == 11)
                    {
                        Submitted_Answers [10] = 1;
                    }
                   if (QuestionNumber == 12)
                    {
                        Submitted_Answers [11] = 1;
                    }
                   if (QuestionNumber == 13)
                    {
                        Submitted_Answers [12] = 1;
                    }
                   if (QuestionNumber == 14)
                    {
                        Submitted_Answers [13] = 1;
                    }
                   if (QuestionNumber == 15)
                    {
                        Submitted_Answers [14] = 1;
                    }
                   if (QuestionNumber == 16)
                    {
                        Submitted_Answers [15] = 1;
                    }
                   if (QuestionNumber == 17)
                    {
                        Submitted_Answers [16] = 1;
                    }
                   if (QuestionNumber == 18)
                    {
                        Submitted_Answers [17] = 1;
                    }
                   if (QuestionNumber == 19)
                    {
                        Submitted_Answers [18] = 1;
                    }
                   if (QuestionNumber == 20)
                    {
                        Submitted_Answers [19] = 1;
                    }
                     
                    
            }
             
            if (mouse.getX() >= 481 &&
                mouse.getX() <= 649 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                //Greenfoot.setWorld(new question4());
                QuestionNumber += 1;
                //System.out.println( QuestionNumber );
                   if (QuestionNumber == 1)
                    {
                        Submitted_Answers [0] = 1;
                         
                    }
                   if (QuestionNumber == 2)
                    {
                        Submitted_Answers [1] = 2;
                    }
                   if (QuestionNumber == 3)
                    {
                        Submitted_Answers [2] = 2;
                    }
                   if (QuestionNumber == 4)
                    {
                        Submitted_Answers [3] = 2;
                    }
                   if (QuestionNumber == 5)
                    {
                        Submitted_Answers [4] = 2;
                    }
                   if (QuestionNumber == 6)
                    {
                        Submitted_Answers [5] = 2;
                    }
                   if (QuestionNumber == 7)
                    {
                        Submitted_Answers [6] = 2;
                    }
                   if (QuestionNumber == 8)
                    {
                        Submitted_Answers [7] = 2;
                    }
                   if (QuestionNumber == 9)
                    {
                        Submitted_Answers [8] = 2;
                    }
                   if (QuestionNumber == 10)
                    {
                        Submitted_Answers [9] = 2;
                    }
                   if (QuestionNumber == 11)
                    {
                        Submitted_Answers [10] = 2;
                    }
                   if (QuestionNumber == 12)
                    {
                        Submitted_Answers [11] = 2;
                    }
                   if (QuestionNumber == 13)
                    {
                        Submitted_Answers [12] = 2;
                    }
                   if (QuestionNumber == 14)
                    {
                        Submitted_Answers [13] = 2;
                    }
                   if (QuestionNumber == 15)
                    {
                        Submitted_Answers [14] = 2;
                    }
                   if (QuestionNumber == 16)
                    {
                        Submitted_Answers [15] = 2;
                    }
                   if (QuestionNumber == 17)
                    {
                        Submitted_Answers [16] = 2;
                    }
                   if (QuestionNumber == 18)
                    {
                        Submitted_Answers [17] = 2;
                    }
                   if (QuestionNumber == 19)
                    {
                        Submitted_Answers [18] = 2;
                    }
                   if (QuestionNumber == 20)
                    {
                        Submitted_Answers [19] = 2;
                    }
            }
             
            if (mouse.getX() >= 762 &&
                mouse.getX() <= 930 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                //Greenfoot.setWorld(new question4());
                QuestionNumber += 1;
                //System.out.println( QuestionNumber );
                   if (QuestionNumber == 1)
                    {
                        Submitted_Answers [0] = 3;
                         
                    }
                   if (QuestionNumber == 2)
                    {
                        Submitted_Answers [1] = 3;
                    }
                   if (QuestionNumber == 3)
                    {
                        Submitted_Answers [2] = 3;
                    }
                   if (QuestionNumber == 4)
                    {
                        Submitted_Answers [3] = 3;
                    }
                   if (QuestionNumber == 5)
                    {
                        Submitted_Answers [4] = 3;
                    }
                   if (QuestionNumber == 6)
                    {
                        Submitted_Answers [5] = 3;
                    }
                   if (QuestionNumber == 7)
                    {
                        Submitted_Answers [6] = 3;
                    }
                   if (QuestionNumber == 8)
                    {
                        Submitted_Answers [7] = 3;
                    }
                   if (QuestionNumber == 9)
                    {
                        Submitted_Answers [8] = 3;
                    }
                   if (QuestionNumber == 10)
                    {
                        Submitted_Answers [9] = 3;
                    }
                   if (QuestionNumber == 11)
                    {
                        Submitted_Answers [10] = 3;
                    }
                   if (QuestionNumber == 12)
                    {
                        Submitted_Answers [11] = 3;
                    }
                   if (QuestionNumber == 13)
                    {
                        Submitted_Answers [12] = 3;
                    }
                   if (QuestionNumber == 14)
                    {
                        Submitted_Answers [13] = 3;
                    }
                   if (QuestionNumber == 15)
                    {
                        Submitted_Answers [14] = 3;
                    }
                   if (QuestionNumber == 16)
                    {
                        Submitted_Answers [15] = 3;
                    }
                   if (QuestionNumber == 17)
                    {
                        Submitted_Answers [16] = 3;
                    }
                   if (QuestionNumber == 18)
                    {
                        Submitted_Answers [17] = 3;
                    }
                   if (QuestionNumber == 19)
                    {
                        Submitted_Answers [18] = 3;
                    }
                   if (QuestionNumber == 20)
                    {
                        Submitted_Answers [19] = 3;
                    }
            }
        }
    }   
}
danpost danpost

2015/5/6

#
Did you not think there might be an easier way to code the class? Repetitive similar code can usually be reduced to simple statements. This is what I see:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Clicker extends Actor
{
    int QuestionNumber;
    int PercentCorrect;
      
    int [] Correct_Answers = { 3, 2, 3, 2, 2, 1, 1, 3, 3, 2, 3, 1, 2, 3, 1, 1, 2, 3, 2, 3 };
    int [] Submitted_Answers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
    public void act()
    {
        if (Submitted_Answers[0] == Correct_Answers[0])
        {
            System.out.println("banana");
        }
        getWorld().setBackground("Question"+(QuestionNumber+1)+".png");
        if (QuestionNumber == 20)
        {
            System.out.println (Submitted_Answers);
            System.out.println (Correct_Answers);
        }
        //List QuestionCounter = getObjects(QuestionCounter.class);
        //if (QuestionCounter.size() == 2) addObject(new QuestionCounter(), 100, 100);
        if (Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            int clicked = 0;
            if (mouse.getX() >= 198 &&
                mouse.getX() <= 366 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                clicked = 1;
            }
            if (mouse.getX() >= 481 &&
                mouse.getX() <= 649 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                clicked = 2;
            }            
            if (mouse.getX() >= 762 &&
                mouse.getX() <= 930 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                clicked = 3;
            }
            if (clicked > 0)
            {
                Submitted_Answers[QuestionNumber] = clicked;
                QuestionNumber++;
            }
        }
    }   
}
My next post will work off of this code (which is essentially the same as your code above).
danpost danpost

2015/5/6

#
Okay. Now, as soon as the first question is answered correctly, lines 11 through 14 will cause the terminal to print "banana" repeatedly and indefinitely. That is not what you want, so remove those lines. Line 15 will repeated set the appropriate image to the actor; yet, it only needs to be set each time a new question is posed. I presume the first question image is set as the default image for the class since you are not setting it in your code. Therefore, the image should be set after my line 51, where the question number is incremented. Place the following lines after line 51 of my code:
1
2
3
4
if (QuestionNumber < 20)
{
    getWorld().setBackground("Question"+(QuestionNumber+1)+".png");
}
and remove line 15. Lines 16 through 20 was your problem code where you were trying to print out the submitted answers with the correct answers. One problem with this is that it will execute prematurely (before the 20th question is answered). Another is it will print repeatedly, until the 20th question is answered and the value of QuestionNumber becomes 21. Even if you corrected the condition to (QuestionNumber == 21), it would then print indefinitely. You either need to change the value of QuestionNumber inside the code-block or (and this next option would be better) move these lines to after where QuestionNumber is incremented (my line 51). The inserted code given above in this post would then be:
1
2
3
4
5
6
7
8
9
if (QuestionNumber < 20)
{ // next question
    getWorld().setBackground("Question"+(QuestionNumber+1)+".png");
}
else
{ // all questions answered
    System.out.println (Submitted_Answers);
    System.out.println (Correct_Answers);
}
We will return to what should actually be in the 'else' code block later. Moving along, the rest of the code, lines 23 through 53, must not be allowed to execute once all questions are answered. Change line 23 to this:
1
if (QuestionNumber < 20 && Greenfoot.mouseClicked(null))
See if this helps and report any problems.
biferony biferony

2015/5/6

#
biferony biferony

2015/5/6

#
I did everything that you said, but I still have the problem that the output is repeating lines of this:
1
2
[I@134f1db
[I@f294c5
Do you know any way to fix that?
danpost danpost

2015/5/6

#
I did state the following about my lines 18 and 19:
We will return to what should actually be in the 'else' code block later.
Now, there are two way to go about printing your results to the terminal. You can print them all out after the last question is answered or you can print them out as the questions are being answered. Which way would you like to proceed with this? As a sidenote, the second way will eliminate the need to revisit the elements in the array for printing the results.
biferony biferony

2015/5/6

#
danpost wrote...
I did state the following about my lines 18 and 19: Now, there are two way to go about printing your results to the terminal. You can print them all out after the last question is answered or you can print them out as the questions are being answered. Which way would you like to proceed with this? As a sidenote, the second way will eliminate the need to revisit the elements in the array for printing the results.
It would be best if they are all printed out at the end. It would just be the more standard way for what I'm doing.
danpost danpost

2015/5/7

#
biferony wrote...
It would be best if they are all printed out at the end. It would just be the more standard way for what I'm doing.
Then replace lines 7 and 8 in my last code post with the appropriate code. Again, you can use a 'for' or 'while' loop to iterate through each element in the array to compare them and print out the appropriate text:
1
2
3
4
5
6
7
8
for (int n=0; n<20; n++)
{
    int qNum = n+1; // question number
    int submitted = Submitted_Answers[n]; // submitted answer to this question
    int actual = Correct_Answers[n]; // correct answer to this question
    boolean match = submitted == actual; // true if this question was answered correctly, else false
    System.out.print(" whatever you wish to output for this question ");
}
biferony biferony

2015/5/7

#
danpost wrote...
biferony wrote...
It would be best if they are all printed out at the end. It would just be the more standard way for what I'm doing.
Then replace lines 7 and 8 in my last code post with the appropriate code. Again, you can use a 'for' or 'while' loop to iterate through each element in the array to compare them and print out the appropriate text:
I did this, but it just printed out 20 of the phrase, even though the arrays only matched around five times. Is there a way to have brackets for the testing if the variables match? I plan to have it print that it is wrong when it is for the else, and also have it increase thee grade when it is correct/true.
danpost danpost

2015/5/7

#
biferony wrote...
I did this, but it just printed out 20 of the phrase, even though the arrays only matched around five times. Is there a way to have brackets for the testing if the variables match? I plan to have it print that it is wrong when it is for the else, and also have it increase thee grade when it is correct/true.
You need to replace the phrase with what you actually wish to have output. You can initialize an int variable to count the number of correct responses before the loop and increment it within the loop when a correct response is found:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int matchCount = 0;
for (int n=0; n<20; n++)
{
    // collect and print data (as above, adjusting what prints)
    if (match)
    {
        matchCount++;
        // output "banana"
    }
    else
    {
        // output "lemon"
    }
}
System.out.println("summary output"); // adjust what prints here also
You can use multiple print lines for any and all output information, if needed. For example:
1
2
3
4
System.out.println("QUESTION "+qNum);
System.out.println("submitted answer: "+submitted);
System.out.println("correct answer: "+actual);
// etc.
biferony biferony

2015/5/7

#
danpost wrote...
You need to replace the phrase with what you actually wish to have output. You can initialize an int variable to count the number of correct responses before the loop and increment it within the loop when a correct response is found:
Thank you so much! I wouldn't have been able to do this without you!
You need to login to post a reply.