Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ReadAllLines not working and it won't tell me why

I’ve finally gotten around to learning Java and I’m trying to write an interpreter for an esolang. I looked up a few tutorials and wrote this code.

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class NDBall {
    public static Scanner scanner = new Scanner(System.in);

    public static void main(String args[]) {
        String input = scanner.nextLine();
        Path path = Paths.get(input);
        List<String> code = new ArrayList<String>();
        code = Files.readAllLines(path);
    }
}

However, the readAllLines function keeps giving me an error, and I don’t know why. It won’t tell me what the error is, and everything else seems fine. I’m doing it exactly as the tutorials I looked up told me.

Is there some mistake I made?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The below code will work for you. have hard coded the path so escape characters are taken care of:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Solution {

    public static Scanner scanner = new Scanner(System.in);

    public static void main(String args[]) {
        String input = scanner.nextLine();
        Path path = Paths.get("C:\\Users\\xyz\\Desktop\\imp.txt");
        List<String> code = new ArrayList<String>();
        try {
            code = Files.readAllLines(path, StandardCharsets.UTF_8);
            code.stream().forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading