当前位置: 首页 > 知识库问答 >
问题:

读取文本文件并将其放入数组

郏志诚
2023-03-14

我读过其他一些类似于此的问题,但都使用了imports和extras,除了importjava.io.*之外,我不想使用任何imports;也没有任何名单。另外,这个is方法中的for循环是我在搞乱它,因为我无法弄清楚它。

public static int[] processFile (String filename) throws IOException, FileNotFoundException {
    BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    String line;
    int[] a = new int[25];
    while (( line = inputReader.readLine()) != null){
        int intValue = Integer.parseInt(line); //converts string into int
        for (int i = 0; i < a.length; i++){
            a[intValue]++;
        }

    }
    return a;
}
 public static void printArray (int[] a) {
    for (int i = 0; i<a.length; i++) {
    System.out.println (a[i]);
    }

}public static void main(string[]args)抛出IOException,FileNotFoundException{int[]array=processFile(“C:\users\griff_000\desktople\testWeek13.txt”);printArray(阵列);}

共有1个答案

潘宸
2023-03-14

我不清楚您的整个导入限制,您到底为什么要限制导入的数量?

不管怎样,查看您的代码,似乎对数组的概念还不是很清楚。

数组是在以下语法下访问的:

array[index] = value;
public static int[] processFile (String filename) throws IOException, FileNotFoundException{
    BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    String line;

    int[] a = new int[25];

    int i = 0; // We need to maintain our own iterator when using a while loop

    while((line = inputReader.readLine()) != null){
        int intValue = Integer.parseInt(line); //converts string into int

        a[i] = intValue; // Store intValue into the array at index i

        i++; // Increment i
    }
    return a;
}
public static int[] processFile (String filename) throws IOException, FileNotFoundException{
    BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    String line;

    int[] a = new int[25];

    for(int i = 0; i < a.length; i++){
        String line = inputReader.readLine(); // Move the readline code inside the loop

        if(line == null){
            // We hit EOF before we read 25 numbers, deal appropriately
        }else{
            a[i] = Integer.parseInt(line); 
        }
    }

    return a;
}
 类似资料:
  • null > 构造函数,它接受两个输入:(String,String)。这两个输入以正确的顺序表示lastname和firstname。构造函数只需将参数中的数据分配给实例变量。 名为toString()的公共实例方法,它返回一个字符串数据(学生的姓名),格式为“lastname,firstname”。 类MainApp null

  • 问题内容: 我已经解决了这些问题,但似乎仍然无法解决。我有一个文本文件,分为几行。每行包含5个数据,中间用“,”分隔。我正在尝试读取此文件并将信息拆分为以下形式的字符串数组: 请有人可以帮我解决一个简单的解决方案!?谢谢!!!:) 数据示例: 样例代码: 公共无效的readFile(){ 错误 07-24 06:26:56.524:E / AndroidRuntime(27203):致命例外:主要

  • 问题内容: 因此,如标题所示,即时消息开始学习一些python,而即时消息处理则难以掌握。我需要完成的工作是读一些数字并将它们存储在列表中。文本文件如下所示: 基本上,这些是用于python的海龟制作形状的坐标和方向。我要讲的是,唯一的问题是使它们采用正确的格式。因此,我无法确定的是如何将这些数字从文件中提取到 A列表中,每个四个坐标是一个大列表中的一个列表。 这是我的尝试,但正如我所说,我需要一

  • 问题内容: 我认为能够将文本文件读入和写出字符串数组的能力是相当普遍的要求。从一种语言开始消除最初访问数据库的需求时,它也非常有用。Golang中是否存在? 例如 和 我宁愿使用现有的而不是重复的。 问题答案: 从Go1.1版本开始,有一个bufio.Scanner API可以轻松读取文件中的行。考虑上面的以下示例,该示例使用Scanner重写:

  • 问题内容: 有什么方法可以读取文本文件并将内容存储在Jtable中?我有一个文本文件,其中包含有关某些过程的某些信息。就像一个具有列和各自值的表。是否可以获取.txt文件的内容并以Jtable的形式显示?我正在使用Eclipse和Window Builder。任何帮助将不胜感激。谢谢! 问题答案: 我将研究Oracle的教程: 读/写文本文件 JTable教程 当从文本文件中获取数据时,您需要将其

  • 问题内容: 我需要读取文件路径为“ C:\ file.pdf”的pdf文件,并将其写入outputStream。最简单的方法是什么? ................................................... ................................................... 问题答案: import java.io.*; 到目前为止