尝试阻止不会一直运行

时间:2016-01-31 10:38:44

标签: java try-catch bufferedreader

我想做的事情 - 创建一个程序来读取文本文件,并基本上将其转换为2D数组。每个单独的角色将占据2D阵列中的一个槽。 我的问题 - 我的代码不会一直在try块中运行。它只是在//System.out.println(" 2");之后的某个时刻停止,我认为它在for循环中。我认为这是问题所在,因为在运行代码时," System.out.println"不能正确匹配我创建的文本文件中的字符数。如果你愿意,可以为自己计算。另外,System.out.println("问题已修复");永远不会跑。这让我觉得try块被退出了,我无法弄清楚原因。似乎没有异常抛出,也没有出现错误。我试着查看,但没有产生任何有用的结果。我将随机数打印到代码中的原因有三个:你的答案的参考点,我的描述的参考点,以及在运行之后精神上的代码。

这是我的代码:

import java.io.*;
import java.util.Scanner;

public class TextGen {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        BufferedReader br = null;
        String line;
        int arrRowSize = 0;
        int arrColSize = 0;
        String currChar;

        System.out.println("Enter file name located in C Drive: ");
        String fileNameEntered = new String(input.nextLine());
        try {
            br = new BufferedReader(new FileReader("C:\\" + fileNameEntered + ".txt"));
        } catch (FileNotFoundException fnfex) {
            System.out.println(fnfex.getMessage() + "File not found");
            System.exit(0);
        }
        // Starting to read lines
        try {
            // Getting 2D array dimensions
            while ((line = br.readLine()) != null) {
                if (line.length() > arrColSize) {
                    arrColSize = line.length();
                }
                arrRowSize += 1;
            }
            String[][] arr = new String[arrRowSize][arrColSize];

            // Filling 2D array with text file
            br = new BufferedReader(new FileReader("C:\\" + fileNameEntered + ".txt"));
            int rowNum = 0;
            int colNum = 0;
            while ((line = br.readLine()) != null) {
                // System.out.println("1");
                if (line != null) {
                    // System.out.println("2");
                    for (int c = 0; c < line.length(); c++) {
                        // System.out.println("3");
                        if (c + 1 == line.length()) {
                            // System.out.println("4");
                            currChar = line.substring(c, line.length());
                        } else {
                            // System.out.println("5");
                            currChar = line.substring(c, c + 1);
                        }
                        // System.out.println("6");
                        arr[rowNum][colNum] = currChar;
                        colNum += 1;
                    }
                }
                rowNum += 1;
            }
            System.out.println("Problem Fixed");
            // Printing 2D array
            for (int row = 0; row < arr.length; row++) {
                System.out.println("inLoop");
                for (int col = 0; col < arr[row].length; col++) {
                    System.out.print(arr[row][col]);
                }
                System.out.println();
            }
        } catch (IOException ioex) {
            System.out.println(ioex.getMessage() + "Error reading file");
        } finally {
            System.out.println("Closed");
            System.exit(0);
        }

    }
}

3 个答案:

答案 0 :(得分:0)

您的colNum不断递增和递增......

您应该在阅读每一行/每行后将colNum重置为0,否则您迟早会得到ArrayIndexOutOfBoundsException

答案 1 :(得分:0)

我在linux机器上试过你的程序,它没有任何问题。修改后的程序是(我只是改变了文件路径)。

import java.io.*;
import java.util.Scanner;
public class test {
public static void main(String [] args) {
    Scanner input = new Scanner(System.in);
    BufferedReader br = null;
    String line;
    int arrRowSize = 0;
    int arrColSize = 0;
    String currChar;

    System.out.println("Enter file name located in current Drive: ");
    String fileNameEntered = new String(input.nextLine());
    try{
        br = new BufferedReader(new FileReader("/home/torreto/prog/java/" + fileNameEntered + ".txt"));
    } catch(FileNotFoundException fnfex){
        System.out.println(fnfex.getMessage() + "File not found");
        System.exit(0);
    }
    //Starting to read lines
    try{
        //Getting 2D array dimensions
            while((line = br.readLine()) != null){
            if(line.length() > arrColSize){
                arrColSize = line.length();
            }
            arrRowSize += 1;
        }
        String[][] arr = new String[arrRowSize][arrColSize];

        //Filling 2D array with text file
        br = new BufferedReader(new FileReader("/home/torreto/prog/java/" + fileNameEntered + ".txt"));
        int rowNum = 0;
        int colNum = 0;
        while((line = br.readLine()) != null){
            //System.out.println("1");
            if(line != null){
                //System.out.println("2");
                for(int c = 0; c < line.length(); c++){
                    //System.out.println("3");
                    if(c+1 == line.length()){
                        //System.out.println("4");
                        currChar = line.substring(c, line.length());
                    } else{
                        //System.out.println("5");
                        currChar = line.substring(c, c+1);
                    }
                    //System.out.println("6");
                    arr[rowNum][colNum] = currChar;
                    colNum += 1;
                }
            }
            rowNum += 1;
        }
        System.out.println("Problem Fixed");
        //Printing 2D array
        for(int row = 0; row < arr.length; row++){
            System.out.println("inLoop");
            for(int col = 0; col < arr[row].length; col++){
                System.out.print(arr[row][col]);
            }
            System.out.println();
        }
    } catch(IOException ioex){
        System.out.println(ioex.getMessage() + "Error reading file");
    } finally{
        System.out.println("Closed");
        System.exit(0);
    }

}
}

输出是:

Enter file name located in current Drive: 
1
Problem Fixed
inLoop
qwerty gg g    gggg
Closed

我认为这个问题在Windows中出现或者是由于文件扩展名(.txt是文本文件的默认值),或者你无法访问c盘中的文件。

答案 2 :(得分:0)

问题是ThomasKläger提到的以及文件中有多行的问题。

尝试捕获Exception,您将看到错误是:java.lang.ArrayIndexOutOfBoundsException

添加colNum = 0; rowNum += 1;所在的位置应解决您的问题。