Explanation regarding skipping of alternate lines while reading from a file?

岁酱吖の 提交于 2020-07-04 02:46:48

问题


I just want to know, have I interpreted the code in right manner?

The file Bharath.txt has the following content:

Hello ! B . We created your file succesfully . We have updated you file , Sir ! Bharath Nikhil Friends Indeed

Consider the below code:

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

class FilInpStr
{
    public static void main(String arg[]) throws Exception
    {
        FileInputStream obj=new FileInputStream("Bharath.txt");
        Scanner input=new Scanner(obj);

        String n="";
        while((n=input.nextLine())!=null)
        {
            System.out.println(n);
        }

    }
}

The above code works well this way. But when I do not consider storing input.nextLine() in a String, it begins to skip line alternatively. Is it that it is taking even the Enter from keyboard as input?

So, when I press Enter, it is passing the escape sequence too, as an input, which is why it is skipping one and printing the other?

And another doubt was regarding the while loop: is that one line wasted while comparing whether it is null or not?

  1. Bharath
  2. Nikhil
  3. Friends Indeed

So, when I quote:

while((input.nextLine()) != null)
{
    System.out.println(input.nextLine());
}

It will check whether Bharath==null or not, and if not, it will only then print Nikhil? And post Nikhil, it will skip next line, because it takes Enter as an escape sequence.

Please, correct me! I might have mixed up two different approaches. A beginner hence.

Also, do mention in distinction the difference between passing the input.nextLine() into a String, and as standalone code.


回答1:


But , when I do not consider storing " input.nextLine()" in a String , it begins to skip line alternatively . Is it , that , it is taking even the ENTER from keyboard as input ?

Well no, it's just reading the next line every time you call input.nextLine(). That shouldn't be surprising. Ignore escape sequences etc. For the sake of simplicity, assume that input.nextLine() is just reading from a list of strings somewhere... it has a cursor showing where on the list it is. Every time you call input.nextLine(), it returns the current line and moves the cursor down.

If you call input.nextLine() twice in each iteration (once in the while condition and once inside the body) then you'll only execute half as many iterations as if you call it once per iteration (as per your original code).




回答2:


input.nextLine()

Does two things :

  1. Return the value of the current line
  2. Advance the cursor past the current line

Scanner.nextLine()

Advances this scanner past the current line and returns the input that was skipped.

So, if you don't store the result of input.nextLine(), it will be lost.

Each call to input.nextLine() advances past the current line, so each call will read a different part of the input.




回答3:


The issue in the second snippet isn't the storing, but the fact you call nextLine() twice in each iteration. First, you call it in the while loop - the line is read and is then ignored (i.e., not saved anywhere), but the internal cursor of the input stream is advanced to the next line. Then, in the println line, you read another line and return it to System.out.println, effectively having all the odd-numbered lines just ignored.



来源:https://stackoverflow.com/questions/34928055/explanation-regarding-skipping-of-alternate-lines-while-reading-from-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!