1

I keep getting an error:

error: variable aryResponse might not have been initialized
                if(answers.charAt(i) == aryResponse[i].charAt(i))

I think it's because I'm initializing the variable within a while loop. However I dont' know how to fix this?

How do I increase the scope of the variable, while I need it to be initiliazed to a value given by the loop?

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

class ExamAnalysis
{
    public static void main(String[] args) throws FileNotFoundException
    {
                    Scanner in = new Scanner(System.in);
                    System.out.println("Welcome to Exam Analysis.  Let's begin ...");
                    System.out.println();
                    System.out.println();
                    System.out.print("Please type the correct answers to the exam questions, one right af$
                    String answers = in.nextLine();
                    int answersLength = answers.length();
                    System.out.println();
                    System.out.print("What is the name of the file containing each student's responses to$
                    String temp = in.nextLine();
                    File file = new File(temp);
                    Scanner in2 = new Scanner(file);
/*Code Relevant To This Question Begins Here */
                    int lines = 0;
                    String[] aryResponse;
                    while (in2.hasNextLine())
                    {
                            String line = in2.nextLine();
                            aryResponse = new String[lines];
                            aryResponse[lines] = line;
                            System.out.println("Student #" + lines + "'s responses:  " + line);
                            lines++;
                    }
                    System.out.println("We have reached \"end of file!\"");
                    System.out.println();
                    System.out.println("Thank you for the data on " + lines + " students.  Here's the ana$
                    int[] aryCorrect = new int[lines];
                    for (int i = 0; i < answersLength; i++)
                    {
                            if(answers.charAt(i) == aryResponse[i].charAt(i))
                            {
                                    aryCorrect[i] ++;
                            }
                    }
       }
}
syhark
  • 35
  • 5

3 Answers3

2

Change this

String[] aryResponse;

to

String[] aryResponse = null;

And remember to test that aryResponse isn't null,

if (aryResponse != null) {
  for (int i = 0; i < answersLength; i++) {
    if (answers.charAt(i) == aryResponse[i].charAt(i)) { // what is this testing?
      aryCorrect[i]++;
    }
  }
}

This is necessary because

while (in2.hasNextLine()) { // <-- might not be true
  // so this doesn't happen.
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks, and also thanks to everyone else as well. However I'm getting an array out of bounds error afterwards, and I can't see why? Also, the third line is comparing two strings character by character. – syhark Jul 31 '14 at 04:36
1

If I understood your code, aryResponse has a number of lines, if the variable was never initialized inside the while loop it means you have 0 lines, so, it would be enough to do two things:

1- initialize aryresponse to null:

String[] aryResponse = null;

2 - add this line at the end of your while loop:

if(aryResponse == null) aryResponse = new String[0];
morgano
  • 17,210
  • 10
  • 45
  • 56
0

Just initialize it outside the loop

String[] aryResponse=null;

Now allocate memory to aryResponse inside loop

aryResponse = new String[n]; //n is the size of array
prince
  • 1,129
  • 1
  • 15
  • 38