Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 17th, 2013, 06:50 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old March 17th, 2013, 08:38 AM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,824 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 8 h 43 m
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
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] ++;

Reply With Quote
  #3  
Old March 17th, 2013, 10:23 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old March 17th, 2013, 10:25 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old March 17th, 2013, 10:53 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old March 17th, 2013, 02:52 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #7  
Old March 17th, 2013, 04:14 PM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old March 17th, 2013, 04:20 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Array won't initialize

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap