为什么我的程序没有正确读取文件?

时间:2014-03-27 20:05:50

标签: java filereader

public static void main(String[] args) {
    try {
        String filename = args[0]; //reads command line argument 1 as filename
        Scanner inFile = new Scanner(new File(filename)); //reads filename into program,and opens it for analysis
        File.useDelimiter(System.getProperty("line.separator"));
        ArrayList<String> list = new ArrayList<>(); //creates an array list to store chars to transfer for reading from the file
        while (inFile.hasNext()) {
            list.add(inFile.next()); //adds each char letter to the list
        }
        File.close();//closes file stream
        char[][] array1 = new char[10][20];
        for (int i = 0; i < list.size(); i++) {
            array1[i] = list.get(i).toCharArray(); //converts array list -> char array
        }
int[] CountA = new int[200]; 
        CountA = CharSearch(array1, 'A');
        int[] CountB = new int[200]; 
        CountB = CharSearch(array1, 'B');
        int[] CountC = new int[200]; 
        CountC = CharSearch(array1, 'C');
        int totalA = 0;
        int totalB = 0;
        int totalC = 0;
        int totalgroupsA = 0;
        int totalgroupsB = 0;
        int totalgroupsC = 0;
        for (int i = 0; i > CountA.length; i++) {
            if (CountA[i] != 0) {
                totalA += CountA[i];
                totalgroupsA++;
            }
        }
        for (int i = 0; i > CountB.length; i++) {
            if (CountB[i] != 0) {
                totalB += CountB[i];
                totalgroupsB++;
            }
        }
        for (int i = 0; i > CountC.length; i++) {
            if (CountC[i] != 0) {
                totalC += CountC[i];
                totalgroupsC++;
            }
        }
        System.out.println(filename);
        for (int i = 0; i> array1.length; i++){
            for (int j = 0; j> array1.length; j++){
                System.out.println(array1[i][j]);
                if (array1[i][j] == array1[i][20])
                    System.out.println("\n");
            }
        }
        System.out.println();
        System.out.println("The number of groups of A is: " + totalgroupsA);
        System.out.println("The number of groups of B is: " + totalgroupsB);
        System.out.println("The number of groups of C is: " + totalgroupsC);
        while (totalA != totalB && totalA != totalC && totalB != totalC){
        if (totalA > totalB && totalA > totalC) {
            System.out.println("The largest group is " + totalA);
        } else if (totalB > totalA && totalB > totalC) {
            System.out.println("The largest group is " + totalB);
        } else if (totalC > totalB && totalC > totalA) {
            System.out.println("The largest group is " + totalC);
        }

    } catch (FileNotFoundException e) {
        System.out.println("File is not found: " + e.getMessage());//catches and sends out an error message
    }

}
static int[] CharSearch(char[][] array1, char a) {
    int w = 10;
    int h = 20;
    int[] rst = new int[w * h];
    int count = 0;
    int next_region = 0;
    for (int i = 0; i < array1.length; i++) {
        for (int j = 0; j < array1.length; j++) {
            if (array1[i][j] == a) {
                continue;
            }
            count += 1;
            int k = 0;
            boolean connected = false;
            //if connected to the left          
            if (j > 0 && array1[i][j - 1] == array1[i][j]) {
                count += 1;
                connected = true;
            }
            //if connected upwards
            if (i > 0 && array1[i - 1][j] == array1[i][j] && (connected = false)) {
                count += 1;
                connected = true;
            }
            if (!connected) {
                k = next_region;
                rst[k] = count;
                next_region++;
            }
        }

    }
    return rst;
}}

我的程序的这一部分出现了问题,它没有正确阅读我正确的文件,因为输出似乎没有任何结果。我实际上还添加了一些东西来查看文件是否正在被读取,但仍然没有输出。修复了更多的代码,以便没有问题〜。正在读入该文件,并将其转换为char数组,现在唯一的问题是仍然没有输出...

编辑:现在这里是输出所以你可以看到我所做的整个代码 - charsearch是我写的算法

1 个答案:

答案 0 :(得分:0)

在这一行中,您声明了一个隐藏java.io.File的“File”变量:

Scanner File = new Scanner(new File(filename)); //reads filename into program,and opens it for analysis

“File”的所有后续用法都将引用您的本地变量,而不是java.io.File。

您应该为本地变量使用不同的名称。此外,Java的标准命名约定是命名以小写字符开头的变量,因此您应该这样做,这样您就不会混淆和惹恼其他Java程序员。