Java 试试方块赢';不要一路跑过去

Java 试试方块赢';不要一路跑过去,java,try-catch,bufferedreader,Java,Try Catch,Bufferedreader,我正在尝试做的是创建一个程序,它将读取一个文本文件,并基本上将其转换为二维数组。每个单独的字符将占用2D数组中的一个插槽。 我的问题-我的代码不会一直在try块中运行。它只是在某个点停止,我相信这是在for循环中,就在//System.out.println(“2”)之后;。我认为这就是问题所在,因为在运行代码时,“System.out.println”与我创建的文本文件中的字符数不匹配。如果你愿意,自己数一数。另外,System.out.println(“问题已解决”);从不跑步。这使我认为t

我正在尝试做的是创建一个程序,它将读取一个文本文件,并基本上将其转换为二维数组。每个单独的字符将占用2D数组中的一个插槽。 我的问题-我的代码不会一直在try块中运行。它只是在某个点停止,我相信这是在for循环中,就在//System.out.println(“2”)之后;。我认为这就是问题所在,因为在运行代码时,“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);
        }

    }
}
import java.io.*;
导入java.util.Scanner;
公共类TextGen{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
BufferedReader br=null;
弦线;
int arrRowSize=0;
int arrColSize=0;
字符串currChar;
System.out.println(“输入位于C驱动器中的文件名:”);
String fileNameEntered=新字符串(input.nextLine());
试一试{
br=新的BufferedReader(新的文件阅读器(“C:\\”+fileNameEntered+“.txt”);
}捕获(FileNotFoundException fnfex){
System.out.println(fnfex.getMessage()+“未找到文件”);
系统出口(0);
}
//开始读台词
试一试{
//获取二维数组维度
而((line=br.readLine())!=null){
if(line.length()>arrColSize){
arrColSize=line.length();
}
arrRowSize+=1;
}
字符串[][]arr=新字符串[arrRowSize][arrColSize];
//用文本文件填充二维数组
br=新的BufferedReader(新的文件阅读器(“C:\\”+fileNameEntered+“.txt”);
int rowNum=0;
int colNum=0;
而((line=br.readLine())!=null){
//系统输出打印项次(“1”);
如果(行!=null){
//系统输出打印项次(“2”);
对于(int c=0;c
您的
colNum
不断递增


读取每行/每行后,应将
colNum
重置为0,否则迟早会出现
ArrayIndexOutOfBoundsException

我在linux机器上尝试了您的程序,它没有任何问题。修改的程序是(我刚刚更改了文件路径)


我认为这个问题发生在windows中,或者是因为文件扩展名(.txt是文本文件的默认值),或者是因为您无法访问c驱动器中的文件。

问题是Thomas Kläger提到的,并且当您的文件中有多行时

尝试捕获异常,您将看到错误为:java.lang.ArrayIndexOutOfBoundsException

添加
colNum=0

其中
rowNum+=1是,应该可以解决您的问题。

您不需要关闭
BufferedReader
s。目前这不是一个问题,但是如果有一个更大的程序,你会遇到问题谢谢!事实上,它确实解决了这个问题。我没有意识到在try/catch中,异常会被忽略。我一想就明白了。但我也要感谢@kaws84。他特别告诉我在哪里修改它,这节省了我一点时间,尽管你先解决了这个问题。
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