1

I am trying to check whether I have reached the last line in the text document to write a simple line in the text document.

I have tried the following code below but I am getting this error:

Exception in thread "main" java.util.NoSuchElementException: No line found

I have text in hello.txt document so I dont understand what this error mean.

How can I fix that?

I appreciate any help.

Code:

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");

    Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.
            if ((line = scanner.nextLine()).isEmpty()) {
                System.out.println("last line in the text document");
                writer.write("last line in the text document.");
            }

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Edit

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");

            Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();

                    // In the case the line is the last one and there is no number.
                    // The content of the arrayList must be printed.
                    if (line.isEmpty()) { // <-- Removed  = scanner.nextLine()
                        System.out.println("last line in the text document");
                        writer.write("last line in the text document.");
                    }else{
                        writer.write(line);
                    }

                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    }

With the edit code I am not getting the error anymore but I am getting this D:\hl_sv\hello.txt line printed in the text document hello2.txt?

Mr Asker
  • 2,120
  • 9
  • 28
  • 49

5 Answers5

1

Updated

I try this code in my computer and its working fine according to your specification.

Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

class Test {

    public static void main(String[] args) {
        try {

            PrintWriter writer = new PrintWriter(new File(
                    "../Test/src/com/hello2.txt"));

            Scanner scanner = new Scanner(new File("../Test/src/com/hello.txt"));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine()+"\n";
                System.out.println(line);
                writer.write(line);
            }

            System.out.println("last line in the text document");
            writer.write("last line in the text document.");
            scanner.close();
            writer.close();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }
    }
}

Edited

You should add new File

new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));

and

new Scanner(new File("D:\\hl_sv\\hello.txt"))); 

Because PritWriter needs File as argument and Scanner needs String or File as argument. As you are using only a String in Scanner ( "D:\\hl_sv\\hello.txt" ) thus you got "D:\hl_sv\hello.txt" line in hello2.txt file.

And you can use a line variable declared immediately after the main function to get access of line variable in main class.

So your code should be:

public static void main(String[] args) {
    String line="";

    try (PrintWriter writer = new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));

    Scanner scanner = new Scanner(new File("D:\\hl_sv\\hello.txt"))) {

        while (scanner.hasNextLine()) {
            line = scanner.nextLine();

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.

        }

        System.out.println("last line in the text document");
        writer.write("last line in the text document.");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

You can check last line of that text file in very simple way / logic just do your operation after the while loop. Because after that while loop in your code there is no line in your text file.

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));

    Scanner scanner = new Scanner(new File("D:\\hl_sv\\hello.txt"))) {

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.

        }

        System.out.println("last line in the text document");
        writer.write("last line in the text document.");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

When you using this statement

if ((line = scanner.nextLine()).isEmpty())

the scanner cannot find any line (after end of the file) so there is no such element (next line) exist so you got this exception

Exception in thread "main" java.util.NoSuchElementException: No line found

Md. Nasir Uddin Bhuiyan
  • 1,568
  • 1
  • 13
  • 22
  • I want to read the file and then write somthing at the end so I need the while statement. – Mr Asker Jun 08 '15 at 17:24
  • Yes you can read a file and then write it in while loop. but you can do it also outside of the while loop. Because the file didn't closed – Md. Nasir Uddin Bhuiyan Jun 08 '15 at 17:28
  • But if I do it outside the while loop then `line` vairable is unknown? Please can you modify your answer to text it with your way. – Mr Asker Jun 08 '15 at 17:32
  • I again updated my answer and showing code which I tried in my computer and it's working to your specification. please check. – Md. Nasir Uddin Bhuiyan Jun 08 '15 at 18:09
  • You have to add this line `writer.println(line);` to the while loop to print the lines of the hello.txt in the new one otherwise it works hanks ;) – Mr Asker Jun 08 '15 at 18:11
0

Please see the linked post for more information.

How to determine when end of file has been reached?

Basically the hasNextLine() function will tell you if you have reached the end of file (which you seem to be using already). I would say rearrange your loop slightly to incorporate this if you want it as a feature.

Community
  • 1
  • 1
Sh4d0wsPlyr
  • 908
  • 12
  • 27
0

You seem to be executing the following

line = scanner.nextLine();

twice per loop.

Given that information, removing one of those calls will solve your problem.

0

Your problem is that you're moving the scanner two lines every time because you call .nextLine() twice.

Fix:

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");

    Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.
            if (line.isEmpty()) { // <-- Removed  = scanner.nextLine()
                System.out.println("last line in the text document");
                writer.write("last line in the text document.");
            }

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
Avantol13
  • 929
  • 10
  • 19
0

Simply test hasNextLine()

        if ( ! scanner.hasNextLine() ) {
            System.out.println("last line in the text document");
            writer.write("last line in the text document.");
        }

A related, rather interesting topic is, if we use Files.lines(path), which is a Stream<String>, how do we detect the last line :)

ZhongYu
  • 18,232
  • 5
  • 28
  • 55