The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Array won't initialize
Discuss Array won't initialize in the Java Help forum on Dev Shed. Array won't initialize Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 17th, 2013, 06:50 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
Homework - Array won't initialize
This code includes an array that I'm trying to assign values to through looping.
This is the whole code:
Code:
public static void grade () throws FileNotFoundException, IOException{
//System.out.println("Grade: " + a + "\n");
String response[] = new String [51];
String correctAnswers [] = {"1","1", "2","2","1","1","3","2","4","1","3","5","4","1","2"};
int numCorrect [][] = new int [17][50];
int count = 0;
//1) open a connection to the file
Scanner inFile = new Scanner (new File ("onePerLine.txt") );
File results = new File ("results.txt");
FileWriter fw;
fw = new FileWriter(results, true);
PrintWriter outFile = new PrintWriter ("results.txt");
PrintWriter outputFile = new PrintWriter(fw);
//accessing QuizScores
File fileName = new File ("QuizScores.txt");
Scanner quizScores = new Scanner(fileName);
// number of lines in oneperline
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("QuizScores.txt")));
lnr.skip(Long.MAX_VALUE);
int numberOfStudents = lnr.getLineNumber();
String [] WID = new String [50];
//number of students
while (quizScores.hasNext()){
String check = quizScores.nextLine();
numberOfStudents += 1;
String [] pieces = check.split(",");
System.arraycopy(pieces, 0, WID, 0, pieces.length -1);
}// while
quizScores.close();
outputFile.println("WID: " + WID[0]);
//2) read input from the file
if (inFile.hasNext()){
for (int x = 1; x<15; x++){
response[x] = inFile.nextLine();
}
System.out.print(response[2]);
//initialize
for (int x = 0; x<17; x++){
for (int y = 0; y<50; y++){
numCorrect[x][y] = 0;
}//inner for.
}
for (int x = 0; x<17; x++){
for (int y = 0; y <50; y++) {
//count correct for each student
if (response[0]!= null && response[0].equals(correctAnswers[0])){
numCorrect[0][x] ++;
}
if (response[1]!= null && response[1].equals(correctAnswers[1])){
numCorrect[0][x] ++;
}
if (response[2]!= null && response[2].equals(correctAnswers[2])){
numCorrect[0][x] ++;
}
if (response[3]!= null && response[3].equals(correctAnswers[3])){
numCorrect[0][x] ++;
}
if (response[4]!= null && response[4].equals(correctAnswers[4])){
numCorrect[0][x] ++;
}
if (response[5]!= null && response[5].equals(correctAnswers[5])){
numCorrect[0][x] ++;
}
if (response[6]!= null && response[6].equals(correctAnswers[6])){
numCorrect[0][x] ++;
}
if (response[7]!= null && response[7].equals(correctAnswers[7])){
numCorrect[0][x] ++;
}
if (response[8]!= null && response[8].equals(correctAnswers[8])){
numCorrect[0][x] ++;
}
if (response[9]!= null && response[9].equals(correctAnswers[9])){
numCorrect[0][x] ++;
}
if (response[10]!= null && response[10].equals(correctAnswers[10])){
numCorrect[0][x] ++;
}
if (response[11]!= null && response[11].equals(correctAnswers[11])){
numCorrect[0][x] ++;
}
if (response[12]!= null && response[12].equals(correctAnswers[12])){
numCorrect[0][x] ++;
}
if (response[13]!= null && response[13].equals(correctAnswers[13])){
numCorrect[0][x] ++;
}
if (response[14]!= null && response[14].equals(correctAnswers[14])){
numCorrect[0][x] ++;
}
}// end inner for
}//end for
} // end if infile.hasnext
outputFile.println(numCorrect[0][0]);
outputFile.println(numCorrect[0][1]);
outputFile.println(numCorrect[0][2]);
//3) close the file
inFile.close();
outFile.close();
outputFile.close();
fw.close();
}//end grade
This is the piece of interest
Code:
while (quizScores.hasNext()){
String check = quizScores.nextLine();
numberOfStudents += 1;
String [] pieces = check.split(",");
System.arraycopy(pieces, 0, WID, 0, pieces.length -1);
}// while
quizScores.close();
outputFile.println("WID: " + WID[0]);
//2) read input from the file
if (inFile.hasNext()){
for (int x = 1; x<15; x++){
response[x] = inFile.nextLine();
}
System.out.print(response[2]);
//initialize
for (int x = 0; x<17; x++){
for (int y = 0; y<50; y++){
numCorrect[x][y] = 0;
}//inner for.
}
for (int x = 0; x<17; x++){
for (int y = 0; y <50; y++) {
//count correct for each student
if (response[0]!= null && response[0].equals(correctAnswers[0])){
numCorrect[0][x] ++;
}
if (response[1]!= null && response[1].equals(correctAnswers[1])){
numCorrect[0][x] ++;
}
if (response[2]!= null && response[2].equals(correctAnswers[2])){
numCorrect[0][x] ++;
}
if (response[3]!= null && response[3].equals(correctAnswers[3])){
numCorrect[0][x] ++;
}
if (response[4]!= null && response[4].equals(correctAnswers[4])){
numCorrect[0][x] ++;
}
if (response[5]!= null && response[5].equals(correctAnswers[5])){
numCorrect[0][x] ++;
}
if (response[6]!= null && response[6].equals(correctAnswers[6])){
numCorrect[0][x] ++;
}
if (response[7]!= null && response[7].equals(correctAnswers[7])){
numCorrect[0][x] ++;
}
if (response[8]!= null && response[8].equals(correctAnswers[8])){
numCorrect[0][x] ++;
}
if (response[9]!= null && response[9].equals(correctAnswers[9])){
numCorrect[0][x] ++;
}
if (response[10]!= null && response[10].equals(correctAnswers[10])){
numCorrect[0][x] ++;
}
if (response[11]!= null && response[11].equals(correctAnswers[11])){
numCorrect[0][x] ++;
}
if (response[12]!= null && response[12].equals(correctAnswers[12])){
numCorrect[0][x] ++;
}
if (response[13]!= null && response[13].equals(correctAnswers[13])){
numCorrect[0][x] ++;
}
if (response[14]!= null && response[14].equals(correctAnswers[14])){
numCorrect[0][x] ++;
}
}// end inner for
}//end for
} // end if infile.hasnext
outputFile.println(numCorrect[0][0]);
outputFile.println(numCorrect[0][1]);
outputFile.println(numCorrect[0][2]);
//3) close the file
inFile.close();
outFile.close();
outputFile.close();
fw.close();
}//end grade
For some reason, No matter what I do, the values that I'm given always stay at 0.
I have no idea why.
Before I was including the loops, it was always above 15.
You may notice there are only 15 iterations. I don't understand what's going on.
|

March 17th, 2013, 08:38 AM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
I'm confused about this code. In the first loop where you initialized them, you used x to control the first index and y to control the second index. Here, you're only changing values in the first array in numCorrect. You aren't using y.
Code:
for (int x = 0; x<17; x++){
for (int y = 0; y <50; y++) {
//count correct for each student
if (response[0]!= null && response[0].equals(correctAnswers[0])){
numCorrect[0][x] ++;
|

March 17th, 2013, 10:23 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
That's because I have devolved into using random experimentations.
I have determined that not initializing isn't the fault of the for loop because its the if statement which is evaluating to false.
When I add on || 2==2) it adds up.
However
It also counts above 19 and I don't see how it is counting more than 15.
Do you think maybe I shouldn't use an array at all for the output?
I just have to have some method of tallying the total correct. But then I'd have to run the loop once per student.
|

March 17th, 2013, 10:25 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
I wanted the loop to go through x and y making one the student and the other the number correct.
|

March 17th, 2013, 10:53 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
Code:
for (int x = 0; x<correctAnswers.length; x++){
for (int y = 0; y <50; y++) {
//count correct for each student
if (response[x]!= null && response[x].equals(correctAnswers[x])){
numCorrect[x][y] ++;
}
I have made it easier to read. Someone suggested I make a single if statement instead of 15.
It should now be going through x as the number of questions and y as the number of students now, no?
but it's still printing 0.
|

March 17th, 2013, 02:52 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
If the two dimensions of the number array are described as rows and columns,
what data is being stored on each row and what in each column on a row?
The if statement inside the for(y) loop does not use the value of y to change any of the expressions used in the if statement.
|

March 17th, 2013, 04:14 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR If the two dimensions of the number array are described as rows and columns,
what data is being stored on each row and what in each column on a row?
The if statement inside the for(y) loop does not use the value of y to change any of the expressions used in the if statement. |
I meant for rows to be the number correct and columns to be students
|

March 17th, 2013, 04:20 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | rows to be the number correct and columns to be students |
In words it would be this. The label at left describes the row, the columns are the student id numbers:
zero correct: Stud#1 Stud#5 Stud#4
one correct: Stud#2
two correct: Stud#3 Stud#6
From the above you could say there was one student that had one correct answer: Stud#2. There were 3 students with no correct answers as shown on the first row.
Last edited by NormR : March 17th, 2013 at 04:22 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|