M
M
Makaron30002021-06-17 12:46:52
Java
Makaron3000, 2021-06-17 12:46:52

Why does it not display the first line when reading from a file?

There is a file test.txt, it contains the words: First line, Second Line. Third Line (To column)
But when reading a file, it shows only Second Line and Third Line

package String;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        String separator = File.separator;
        String path = "C:" + separator + "Users" + separator + "denis" + separator + "Desktop" + separator + "test.txt";
        File file = new File(path);

        Scanner scanner = new Scanner(file);
        String input = scanner.nextLine();
        while(scanner.hasNextLine()){
            System.out.println(scanner.nextLine());
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-06-17
@Makaron3000

Obviously, because the subtracted first line
String input = scanner.nextLine();
is not displayed anywhere, and only the rest are printed in the loop.
System.out.println(scanner.nextLine());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question