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] ++;
}
}
}
}