简体   繁体   English

无法读取和存储文本文件中的每一行

[英]Unable to read and store each line from a text file

I have a text file named " math.txt " which has several math lines.我有一个名为“ math.txt ”的文本文件,它有几个数学行。 These are the lines which are stored in math.txt .这些是存储在math.txt中的行。

1+2+3
1+2+3+4
1+2+3+4+5
1+2+3+4+5+6
1+2+3+4+5+6+7
1+2+3+4+5+6+7+8

I have the code below which is supposed to read out each line from the text file and then store each line in a String array.我有下面的代码,它应该从文本文件中读出每一行,然后将每一行存储在一个字符串数组中。 For some reason, only certain lines are printed, and only certain lines seem to be stored in the array.出于某种原因,只打印了某些行,并且似乎只有某些行存储在数组中。

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

class Main {
  public static void main(String[] args) throws IOException {

    //scanner which scans the text file with math equations
    Scanner file = new Scanner (new File("math.txt"));

    //new string array of of infinite size to read every line
    String [] lines = new String [100000];

    //int to count how many lines the text file has to be used in a future for loop
    int lineCount = -1;    


    System.out.println("\nPrint each line from text file");


    //if there is a line after the current line, the line count goes up and line is stored in the initial array
    while (file.hasNextLine()){
        System.out.println(file.nextLine());
        lineCount++;
        lines[lineCount] = file.nextLine();
    }

    System.out.println("\nLines in array");

    for(int i=0;i<lineCount; i++){
        System.out.println(lines[i]);
    }

  }
}

The output should be输出应该是

Print each line from text file
1+2+3
1+2+3+4
1+2+3+4+5
1+2+3+4+5+6
1+2+3+4+5+6+7
1+2+3+4+5+6+7+8

Lines in array
1+2+3
1+2+3+4
1+2+3+4+5
1+2+3+4+5+6
1+2+3+4+5+6+7
1+2+3+4+5+6+7+8

But instead I get the output但相反,我得到了输出

Print each line from text file
1+2+3
1+2+3+4+5
1+2+3+4+5+6+7

Lines in array
1+2+3+4
1+2+3+4+5+6

Where is the issue in my code?我的代码中的问题在哪里?

In your while loop:在你的 while 循环中:

while (file.hasNextLine()){
        System.out.println(file.nextLine());
        lineCount++;
        lines[lineCount] = file.nextLine();
    }

You are calling file.nextLine() twice.您正在调用 file.nextLine() 两次。 This means that lines are read twice for every storage.这意味着每次存储都会读取行两次。 What you will need is something like this:你需要的是这样的:

while( file.hasNextLine()) {
    String s = file.nextLine();
    System.out.println(s);
    lines[lineCount++] = s;
}

Also start your lineCount at 0 instead of -1;也从 0 而不是 -1 开始你的 lineCount;

Take a look at your while block:看看你的while块:

while (file.hasNextLine()){
    System.out.println(file.nextLine());
    lineCount++;
    lines[lineCount] = file.nextLine();
}

Do you see that you use file.nextLine() twice?你看到你使用了file.nextLine()两次吗? Each call to nextLine() advances the cursor, so while you intend to use the current line twice, the second call to nextLine() skips to the next line.每次调用nextLine()使光标前进,因此当您打算使用当前行两次时,第二次调用nextLine()跳到下一行。

What you should do is store the line in a local field, and just use that field directly.您应该做的是将行存储在本地字段中,然后直接使用该字段。

while (file.hasNextLine()){
    String foo = file.nextLine();
    System.out.println(foo);
    lineCount++;
    lines[lineCount] = foo;
}

The problem happens because you are reading 2 lines on every loop iteration.出现问题是因为您在每次循环迭代中读取 2 行。 Every每一个

file.nextLine()文件.nextLine()

is reading another line in the file.正在读取文件中的另一行。

You can try with this:你可以试试这个:

 while (file.hasNextLine()){
    lineCount++;
    lines[lineCount] = file.nextLine();
    System.out.println(lines[lineCount]);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM