Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将输入文件中的每一行添加到数组中_Java_Arrays_File Io - Fatal编程技术网

Java 将输入文件中的每一行添加到数组中

Java 将输入文件中的每一行添加到数组中,java,arrays,file-io,Java,Arrays,File Io,我需要将用户输入文本文件的每一行(单个单词)的内容添加到数组中的单独元素中。 *我知道对于这个问题,ArrayList是一个更好的数据结构,但我仅限于使用数组 这是我的密码: public static void main(String[] args) throws FileNotFoundException { Scanner scanner = new Scanner(System.in); System.out.print("Enter a file name: ");

我需要将用户输入文本文件的每一行(单个单词)的内容添加到数组中的单独元素中。 *我知道对于这个问题,ArrayList是一个更好的数据结构,但我仅限于使用数组

这是我的密码:

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a file name: ");
    System.out.flush();
    String filename = scanner.nextLine();
    File file = new File(filename);
    FileReader reader = new FileReader(file);
    try (BufferedReader buffReader = new BufferedReader(reader)) {
        String line;
        int i=0;
        String[] words = new String[10];
        while((line = buffReader.readLine()) != null) {
            words[i]=buffReader.readLine();
            System.out.println(words[i]);
            i++;
        }
    }
    catch(IOException e){
        System.out.println(e);
    }
}
输入文件仅为:

Pans 
Pots 
opt 
sit 
it's
snap
程序输出如下。它似乎每隔一行就跳过一行

Pots
sit
snap

每次
while
循环迭代读取两行,一行在
while
条件下,另一行在循环体的第一行。结果是每次迭代消耗两行,并且只打印两行中的第二行


消除循环体中的第二个调用,以便每行有一次循环迭代(和一条打印语句)。

在循环迭代时,每
读取两行,一行在
while
条件下,另一行在循环体的第一行。结果是每次迭代消耗两行,并且只打印两行中的第二行


消除循环体中的第二个调用,这样每行就有一次循环迭代(和一条打印语句)。

更改while循环,如下所示。如果while循环条件检查,则您正在读取该行,然后再次读取while循环第一行中的行。所以你读了两行,只打印了一行

 while((line = buffReader.readLine()) != null) {
        System.out.println(line );
        i++;
 }

更改while循环,如下所示。如果while循环条件检查,则您正在读取该行,然后再次读取while循环第一行中的行。所以你读了两行,只打印了一行

 while((line = buffReader.readLine()) != null) {
        System.out.println(line );
        i++;
 }
该行读取并使用了第一个输入行,这就是为什么您没有看到Pans和甚至索引的输入


该行读取并使用了第一个输入行,这就是为什么您没有看到Pans和甚至索引的输入

除了使用toArray方法将ArrayList转换为数组之外,您还可以使用ArrayList吗?您的限制是什么?为什么?

除了使用toArray方法将其转换为数组之外,您还可以使用ArrayList吗?你的局限性是什么?为什么

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a file name: ");
    System.out.flush();
    String filename = scanner.nextLine();
    File file = new File(filename);
    FileReader reader = new FileReader(file);
    try (BufferedReader buffReader = new BufferedReader(reader)) {
        String line;
        int i=0;
        String[] words = new String[10];
        //modify line = buffReader.readLine()
        while((words[i]=buffReader.readLine()) != null) {
                //modify //words[i]=buffReader.readLine();
            //words[i]=buffReader.readLine();
            System.out.println(words[i]);
            i++;
        }
    }
    catch(IOException e){
        System.out.println(e);
    }
}