Hi, could someone tell me what's wrong.

The code works for the data set, answers but doesn't for data set answers2... the program crashes after displaying the data set2 and never gets data set 3, I will appreciate any help. Thanks.

import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.lang.*;

public class Answers
{
	public static void main (String[] args)
    {
		int[] student = { 1080, 1340, 1341, 1401, 1462, 1463, 1464, 1512, 1618, 1619, 1687, 1700, 1712, 1837 };
		int[] student2 = { 2080, 1540, 1321, 5401, 1422, 1163, 1404 };
		int[] student3 = { 7777 };

		char[][] answer = { {'F','T','F','F','T','F','F','T','F','T'},
							{'F','T','T','F','T','F','T','T','F','T'},
							{'F','T','F','T','F','T','T','T','F','F'},
							{'F','T','T','F','T','T','T','T','T','T'},
							{'T','T','F','F','T','F','F','T','T','T'},
							{'T','T','F','T','T','T','F','F','T','F'},
							{'T','T','T','T','T','T','T','T','T','T'},
							{'F','T','F','F','F','T','F','T','F','T'},
							{'T','F','T','F','T','F','T','F','T','F'},
							{'T','T','T','F','F','T','T','F','T','F'},
							{'F','F','F','F','F','F','F','F','F','F'},
							{'T','F','T','T','F','T','T','F','T','F'},
							{'F','T','F','F','T','T','F','F','F','T'},
							{'F','T','F','T','F','T','F','T','F','T'},
							{'T','F','T','F','T','T','F','T','F','T'}  };

		char[][] answer2 = {{'F','T','F','F','T','F','F','T','F','T'},
							{'F','T','T','F','T','F','T','T','F','T'},
							{'F','T','F','T','F','T','T','T','F','F'},
							{'F','T','T','F','T','T','T','T','T','T'},
							{'T','T','F','F','T','F','F','T','T','T'},
							{'T','T','F','T','T','T','F','F','T','F'},
							{'T','T','T','T','T','T','T','T','T','T'},
							{'F','T','F','F','F','T','F','T','F','T'} };

		char[][] answer3 = { {'F','T','F','F','T','F','F','T','F','T'},
							 {'T','T','F','F','T','F','F','T','F','T'} };

		/*Answers comp1130 = new Answers();
		Answers comp1230 = new Answers();
		Answers comp2120 = new Answers();*/

		System.out.println ("Comp1130\n");
		studentMarks (answer, student);
		howManyGotEachQ (answer, student);

		System.out.println ("Comp1230\n");
		studentMarks (answer2, student2);
		howManyGotEachQ (answer2, student2);

		System.out.println ("Comp2120\n");
		studentMarks (answer3, student3);
		howManyGotEachQ (answer3, student3);

    }//end of main method

	/*public Answers ()
	{

	}*/

    /*public Answers (char[][] answer, char[][] answer2, char[][] answer3, int[] student, int[] student2, int[] student3)
    {
    	//Answers constructor

    	answer = new char[][] {  {'F','T','F','F','T','F','F','T','F','T'},
					{'F','T','T','F','T','F','T','T','F','T'},
					{'F','T','F','T','F','T','T','T','F','F'},
					{'F','T','T','F','T','T','T','T','T','T'},
					{'T','T','F','F','T','F','F','T','T','T'},
					{'T','T','F','T','T','T','F','F','T','F'},
					{'T','T','T','T','T','T','T','T','T','T'},
					{'F','T','F','F','F','T','F','T','F','T'},
					{'T','F','T','F','T','F','T','F','T','F'},
					{'T','T','T','F','F','T','T','F','T','F'},
					{'F','F','F','F','F','F','F','F','F','F'},
					{'T','F','T','T','F','T','T','F','T','F'},
					{'F','T','F','F','T','T','F','F','F','T'},
					{'F','T','F','T','F','T','F','T','F','T'},
					{'T','F','T','F','T','T','F','T','F','T'}  };

		answer2 = new char[][] { {'F','T','F','F','T','F','F','T','F','T'},
					{'F','T','T','F','T','F','T','T','F','T'},
					{'F','T','F','T','F','T','T','T','F','F'},
					{'F','T','T','F','T','T','T','T','T','T'},
					{'T','T','F','F','T','F','F','T','T','T'},
					{'T','T','F','T','T','T','F','F','T','F'},
					{'T','T','T','T','T','T','T','T','T','T'},
					{'F','T','F','F','F','T','F','T','F','T'} };

		answer3 = new char[][] { {'F','T','F','F','T','F','F','T','F','T'},
					{'T','T','F','F','T','F','F','T','F','T'} };


		student = new int[] { 1080, 1340, 1341, 1401, 1462, 1463, 1464, 1512, 1618, 1619, 1687, 1700, 1712, 1837 };
		student2 = new int[]	{ 2080, 1540, 1321, 5401, 1422, 1163, 1404 };
		student3 = new int[]	{ 7777 };

    }//end of constructor*/


	public static void studentMarks (char[][] ans, int[] stud)
	{
		//This method calculates and diplays the marks for each student.

		int correct = 0;

		final int STUDENTS = ans.length - 1;//calculates the number of students

		int[] mark = new int[STUDENTS];//creating an array to store the number of correct answers for each student

		for (int i=1; i<ans.length; i++)
		{
			for (int j=0; j<ans[i].length; j++)
			{
				if ((ans[i][j]) == (ans[0][j]))
					correct++;
			}
			mark[i-1] = correct;//stores the mark in the mark array
			correct = 0;//resets the counter; sets it to 0
		}

		displayMarks (ans[0].length, stud, mark);//sends the computed data to the display method.
	}

	public static void displayMarks (int ans, int[] stud, int[] holdMarks)
	{
		//This method displays the student numbers and the mark each student got.

		System.out.println ("Student#\tMark(out of " +ans +")");

		for (int i=0; i<holdMarks.length; i++)
		{
			System.out.print ("  " +stud[i] +"\t\t\t  " +holdMarks[i] +"\n");
		}
	}

	public static void howManyGotEachQ (char[][] ans, int[] stud)
	{
		//This method calculates and diplays how many students got each question right and the correct answer for each question.

		int correct = 0;
		int incorrect = 0;

		final int STUDENTS = ans[0].length;//calculates the number of questions

		int[] cor = new int[STUDENTS];//creating an array to store the number of correct answers for each student
		int[] incor = new int[STUDENTS];//creating an array to store the number of correct answers for each student

		for (int i=1; i<=ans[0].length; i++)
		{
			for (int j=0; j<ans[0].length; j++)
			{
				if ((ans[i][j]) == (ans[0][j]))
					correct++;
				else
					incorrect++;
			}
			cor[i-1] = correct;//stores the correct number of answers in the cor array
			incor[i-1] = incorrect;//stores the incorrect number of answers in the incor array

			correct = 0;//resets the correct counter; sets it to 0
			incorrect = 0;//resets the incorrect counter; sets it to 0
		}

		displayQM (ans[0], cor, incor);//sends the computed data to the display method
	}

	public static void displayQM (char[] correct, int[] holdCor, int[] holdIncor)
	{
		//This method displays the number of correct and incorrect answers for each question.

		System.out.println ("\nQuestion\tCorrect\t Number\t Number");
		System.out.println ("Number\t\tAnswer\t Correct Incorrrect");

		for (int i=1; i<=correct.length; i++)
		{
			if (i<10)
				System.out.print ("  " +(i) +"\t\t\t   " +correct[i-1] +"\t\t" +holdCor[i-1] +"\t\t" +holdIncor[i-1] +"\n");
			else//the else statement removes the extra spaces that results from the number 10, hence helping with formatting
				System.out.print ("  " +(i) +"\t\t   " +correct[i-1] +"\t\t" +holdCor[i-1] +"\t\t" +holdIncor[i-1] +"\n");
		}
	}


}//end of Answers class

The code looks cryptic. Could u shorten the comment lines and remove the methods that u dont want in ur classs?

Edited 5 Years Ago by stevanity: n/a

"crashes" tells us nothing. Do you get an error or Exception message? If so please post the whole thing, including the line number(s).
EDIT: Sorry sorry sorry - just went back to main forum listing and saw your post's title. Sorry.
I'm looking at the problem now...

Line 111, why length-1?

Edited 5 Years Ago by JamesCherrill: n/a

Thanks for your replies; the message it displays is this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
	at Answers.howManyGotEachQ(Answers.java:171)
	at Answers.main(Answers.java:66)

On my screen, that line is the one that starts:

if ((ans[i][j]) == (ans[0][j]))
					correct++;
				else
					incorrect++;

"crashes" tells us nothing. Do you get an error or Exception message? If so please post the whole thing, including the line number(s).
EDIT: Sorry sorry sorry - just went back to main forum listing and saw your post's title. Sorry.
I'm looking at the problem now...

Line 111, why length-1?

Not sure why it's needed either, but without it, the last line of my array initialization list/data doesn't get displayed when I call one of the two display methods. :/

OK that helps. Add a print statement in the loop to see if its i or j which is going out of range.

ps following the length-1 question, why are student arrays 1 shorter than the answer arrays? And could this cause the problem - ie 8 answers but only 7 students?

OK that helps. Add a print statement in the loop to see if its i or j which is going out of range.

ps following the length-1 question, why are student arrays 1 shorter than the answer arrays? And could this cause the problem - ie 8 answers but only 7 students?

Ok thanks... I'll try that.

The second and third data set (answers2 and answers3) are to test that the program works in all scenarios... the program works fine for the array answers without any exception though.

The code looks cryptic. Could u shorten the comment lines and remove the methods that u dont want in ur classs?

The edit button disappeared from my post... sorry I can't edit it.

The no off students in 1st data is > number of questions. in 2nd & 3rd its smaller. Maybe you are confusing the questions index/loop values with the students one somewhere??? (ie looping thru 10 elements (no of questions) to access students (OK for 14 students, out of bounds for 9 students)???

I assume the first row in the answers correspond to correct answers? Thats why u do length-1??

OK that helps. Add a print statement in the loop to see if its i or j which is going out of range.

ps following the length-1 question, why are student arrays 1 shorter than the answer arrays? And could this cause the problem - ie 8 answers but only 7 students?

Following your advice, I realized that j is going out of bounds. I doesn't go beyond 7, but j goes to 9.

Could I be using the wrong length of an array to control the for loop for j?
Thanks

I assume the first row in the answers correspond to correct answers? Thats why u do length-1??

And that's why it compares the subsequent rows in a loop with row 0?
Stevanity - i think you're a genius.
Schoolboy - did your teacher tell you about using comments to explain your code and data structures?

Edited 5 Years Ago by JamesCherrill: n/a

I assume the first row in the answers correspond to correct answers? Thats why u do length-1??

Yes it is.
So I'm basically trying to compare everything else with the data set in the first row.

I think the entire looping structure that ure using to compute number of correct answers for each qn itself is totally messed up. Think over it and restructure it. And for heaven's sake, name ur variables properly.

The STUDENTS variable inside howManyGotEachQ corresponds to number of Questions right?? Why have u named it as STUDENTS??

And that's why it compares the subsequent rows in a loop with row 0?
Stevanity - i think you're a genius.
Schoolboy - did your teacher tell you about using comments to explain your code and data structures?

lol yes... I should have added it to the code since you're not my teacher lol
Completely skipped my mind... apologies

Use this pseudocode for the howManyGotEachQ method:

set i to 0 //control for moving across questions
  set j to 1 //control for moving thru each student
     if jth student has ith qn correct increment "correct"
     else increment incorrect
  incr j upto ans.length
incr i upto ans[0].length

I can confirm everything else is perfectly OK.

Use this pseudocode for the howManyGotEachQ method:

set i to 0 //control for moving across questions
  set j to 1 //control for moving thru each student
     if jth student has ith qn correct increment "correct"
     else increment incorrect
  incr j upto ans.length
incr i upto ans[0].length

I can confirm everything else is perfectly OK.

Okay, thanks, I'm trying that as I'm typing this.
Brb

Check out line 153 in your original posted code

for (int i=1; i<=ans[0].length; i++)

I think that's your error.
It causes you to use the no of questions to control an index that you use for students (as per my previous hypothesis)
And may follow from the terrible var name on line 148 as per Stevanity's previous comment.

Edited 5 Years Ago by JamesCherrill: n/a

Check out line 153 in your original posted code

for (int i=1; i<=ans[0].length; i++)

I think that's your error.
It causes you to use the no of questions to control an index that you use for students (as per my previous hypothesis)
And may follow from the terrible var name on line 148 as per Stevanity's previous comment.

aaaah... I see your point... yeah, I'll change that to QUESTIONS instead.

Use this pseudocode for the howManyGotEachQ method:

set i to 0 //control for moving across questions
  set j to 1 //control for moving thru each student
     if jth student has ith qn correct increment "correct"
     else increment incorrect
  incr j upto ans.length
incr i upto ans[0].length

I can confirm everything else is perfectly OK.

I've tried and I don't seem to get it to work better.
And if I understood your reply, you're saying that the only thing wrong with my code (at least logically) for it to work is that method or rather, the for loops?

Edited 5 Years Ago by schoolboy2010: n/a

Did you change line 153? Did it help?

Yes I did... and following your advice and Stevanity's, here's my code:

for (int i=0; i<ans.length; i++)
		{
			for (int j=1; j<ans[0].length; j++)
			{
				if ((ans[j][i]) == (ans[0][j]))
					correct++;
				else
					incorrect++;

				System.out.print (j);
			}

Thing is, now, it doesn't even work perfectly fine for the first data set, "answer" :/

No no. ure getting it wrong again.

i - corresponds to the questions... and so... it must run from 0 to ans[0].length

and

j - corresponds to the students... and so... it must run from row 1 to row ans.length-1. (j<ans.length)

Do that. and since i corresponds to question row. you need to check each ans with ans[0] and not ans[0][j]

---

and BTW, i can confirm 100% that everything else is OK.

Edited 5 Years Ago by stevanity: n/a

No no. ure getting it wrong again.

i - corresponds to the questions... and so... it must run from 0 to ans[0].length

and

j - corresponds to the students... and so... it must run from row 1 to row ans.length-1. (j<ans.length)

Do that. and since i corresponds to question row. you need to check each ans with ans[0] and not ans[0][j]

---

and BTW, i can confirm 100% that everything else is OK.

Thanks for your reply Stevanity.
I tried to change what you mentioned and still get an arrayoutofboundsException

Here's my updated code:

for (int i=0; i<=ans[0].length; i++)
{
	for (int j=1; j<ans.length; j++)
	{
		if ((ans[i][j]) == (ans[0][i]))
			correct++;
		else
			incorrect++;
		System.out.print (j);//for debugging
}

You're really getting your indexes and lengths crossed up here.
If you are going to have the conventional [j] inside the loop then the limit for i is array.length, and the limit for j is array.length
... and v.v. if you really must use [j] (confusing)

If this is leading to a brain meltdown then I suggest the following:
1. explicitly document which index is student and which is question.
2. create variables for the number of students and number of questions
3. call your loop vars student and question rather than i and j
thus making it look like:

// Array is indexed by [student number][question number]
for (int student = 1; student < numberOfStudents; student++) {
  for (int question = 0; question < numberOfQuestions; question++) {
     array[student][question] = etc

now it should be really obvious whether the code is right or wrong.

Like Stevanity said - "name ur variables properly". I agree - good naming makes it so much easier to understand code and avoid bugs.

Thanks Stevanity and James Cherrill
I eventually got it solved.... well sorta kinda.... I got it to display everything up to the answer3 data set, so I'm good with that.
Thanks a lot for your help once more :)

This question has already been answered. Start a new discussion instead.