2

对于家庭作业,我需要创建一个可以从文件读取和写入字节数组的类。我已经成功创建了可以读写 CSV 和文本的类,但是在涉及数组时我遇到了一些困难。

当我运行“readByte”方法(见下文)时,我没有收到编译器错误,而是无法获取要打印的文件内容。相反,控制台只显示“文件读取”,表明它已成功处理嵌套的 for 循环。我研究了各种资源,但找不到解决方案。我确信这是某个地方的一个简单错误,任何关于我如何解决这个问题的建议将不胜感激。

我试图阅读的文件的内容也在下面。

第二个问题(我认为最好将两个问题放在一篇文章中),在我的“writeByte”方法中(用户将值输入到二维数组中,然后打印在 .txt 文件中),允许我输入两个在我收到以下错误消息之前的值:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.gc01.FileManager.ByteManager.writeByte(ByteManager.java:93)
at com.gc01.FileManager.ByteManager.main(ByteManager.java:111)

我确信这与 for 循环如何收集用户输入有关,但我不知道如何纠正它。理想情况下,该文件看起来类似于“readByte”方法正在读取的文件。

我理解这是一个很长的问题,但我遇到了一堵砖墙,所有帮助将不胜感激!


文件内容(由选项卡分隔)


1     10
2     11
3     12
4     13

public class ByteManager {

public String getByteFile(){
    Scanner sc = new Scanner (System.in);
    System.out.println("Please enter the file directory of the chosen txt file?");
    System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
    ///Users/ReeceAkhtar/Desktop/FileName.txt
    final String fileName = sc.nextLine();
    System.out.println("How many columns are in the file?");
    final int columns = sc.nextInt();
    System.out.println("How many rows are in the file?");
    final int rows = sc.nextInt();
    return fileName;
    }



public void readByte(final String fileName, int rows,int columns){

BufferedReader br = null;

String[] line;
String splitBy =  "\t";
int [][] data = new int[rows] [columns];


try {
    br = new BufferedReader(new FileReader(fileName));  
        for (int i = 0; i < rows; i++){
            line = br.toString().split(splitBy);
            //data[i] [0] = Integer.parseInt(line[i]);
            for (int j = 0; j < columns; j++){
                data[i] [j] = Integer.parseInt(line[j]);
                System.out.println(data[i][j]);
            }
        }       
    } catch (FileNotFoundException e){
        e.printStackTrace();
    } catch (IOException e) {   
        e.printStackTrace();  
    } finally {  
        if (br != null) {  
        try {  
        br.close();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }
        }
    }
    System.out.println("*****File Read*****");
}


public String chooseFileOutput(){
    Scanner sc = new Scanner (System.in);
    System.out.println("Please enter the file directory for the output of the chosen file");
    System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
    ///Users/ReeceAkhtar/Desktop/GeoIPCountryWhois.csv
    final String fileNameOUT = sc.nextLine();
    return fileNameOUT;
    }

public void writeByte(final String fileNameOUT){
    Scanner input = new Scanner (System.in);
    FileOutput createData = new FileOutput (fileNameOUT);
    System.out.println("How many rows?");
    int rowsOut = input.nextInt();
    System.out.println("How many columns?");
    int columnsOut = input.nextInt();


    System.out.println("Please enter data.");

        int newDataR = 0;
        int newDataC = 0;
        int [] [] data = new int [rowsOut] [columnsOut];    


        for (int i = 0; i < rowsOut; i++){
            createData.writeInteger(newDataR = input.nextInt());
            System.out.print("\t");
            for (int j = 0; j < columnsOut; i++){
                createData.writeInteger(newDataC = input.nextInt());
                data[i] [j] = data[newDataR] [newDataC];
                System.out.print("\t");
            } 
        }   
        createData.close();

}

public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    final ByteManager object = new ByteManager ();

    System.out.println("1 for Read File, 2 for Write file");
    String choice = in.nextLine();
    if("1".equals(choice)){
        object.readByte(object.getByteFile(), 0, 0);
    } else if ("2".equals(choice)){
        object.writeByte(object.chooseFileOutput());
    } else{
        System.out.println("Goodbye!");
        System.exit(0);
    }
}
}
4

3 回答 3

1

对于您的第一个问题(关于无法阅读):

在该main方法中,以下行:

object.readByte(object.getByteFile(), 0, 0);

为要从文本文件中读取的行数和列数提供 0 和 0。该方法getByteFile()确实会提示用户输入一些行和列,但是它不会返回这个数量并且对这些数字不做任何事情。您必须提示用户输入行数和列数,并将它们作为readByte方法的第二个和第三个参数放在main.

另外,一般来说,我想知道您为什么费心int[][] data在您的readBytewriteByte方法中创建对象。它们是 void 方法,因此您不会返回int[][], 或对它做任何有意义的事情。你打算以后使用这个对象吗?

于 2013-10-29T15:31:22.967 回答
0

“第 93 行是:'data[i] [j] = data[newDataR] [newDataC];”

好的,那是你的问题。我认为你的意思是:

data[i][j] = newDataC;

多维数组只是数组的数组。当您有这样的访问权限时:

data[i][j]

这意味着您正在访问数组#i 的元素#j。

data[][]是 int 数组的数组,data[i]是 int 数组,并且data[i][j]是 int 中的元素data[i]

因此,我建议查看多维数组的工作原理,然后通常查看您的代码以查找如下内容:

createData.writeInteger(newDataR = input.nextInt());

这基本上看起来很荒谬。不意味着负面评论,这一行在多维数组的上下文中没有意义,因为只有“列”包含 int 元素。

于 2013-10-29T15:39:45.333 回答
0

对于您的例外:我不确定我的评论是否清楚,所以我会在这里尝试。你有:

for (int i = 0; i < rowsOut; i++){
    createData.writeInteger(newDataR = input.nextInt());
    System.out.print("\t");
    for (int j = 0; j < columnsOut; i++){  //do you mean to increment j here?
        createData.writeInteger(newDataC = input.nextInt());
        data[i] [j] = data[newDataR] [newDataC];
        System.out.print("\t");
    } 
} 

在您的第二个 for 循环中,您正在递增i,而不是j。你将它增加两次,所以你会去 0,2,4...所以你几乎肯定会超过你的数组的那个维度。正如 Radiodef 所说,我认为您将从阅读多维数组中受益。

于 2013-10-29T15:52:34.593 回答