#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2

    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.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    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] ++;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    I wanted the loop to go through x and y making one the student and the other the number correct.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    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.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    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
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    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.

IMN logo majestic logo threadwatch logo seochat tools logo