变量可能尚未初始化...如何增加范围?

时间:2014-07-31 04:18:59

标签: java arrays

我一直收到错误:

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

我认为这是因为我在while循环中初始化变量。但是我不知道如何解决这个问题?

如何增加变量的范围,而我需要将其初始化为循环给出的值?

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

3 个答案:

答案 0 :(得分:2)

更改此

String[] aryResponse;

String[] aryResponse = null;

请记住测试aryResponse不是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]++;
    }
  }
}

这是必要的,因为

while (in2.hasNextLine()) { // <-- might not be true
  // so this doesn't happen.
}

答案 1 :(得分:1)

如果我理解你的代码,aryResponse有许多行,如果变量从未在while循环中初始化,则意味着你有0行,所以,做两件事就足够了:

1-将aryresponse初始化为null:

String[] aryResponse = null;

2 - 在while循环结束时添加此行:

if(aryResponse == null) aryResponse = new String[0];

答案 2 :(得分:0)

只需在循环外部初始化

String[] aryResponse=null;

现在将内存分配给循环内的aryResponse

aryResponse = new String[n]; //n is the size of array