Scanner only reads file name and nothing else

前端 未结 1 1511
小鲜肉
小鲜肉 2020-11-28 15:54

I\'m trying to implement a rudimentary lexer. I\'m stuck on the file parsing at the moment.

public ArrayList ParseFile () {

    int lineIndex =         


        
相关标签:
1条回答
  • 2020-11-28 16:33

    You've misunderstood the API for Scanner. From the docs for the Scanner(String) constructor:

    Constructs a new Scanner that produces values scanned from the specified string.

    Parameters:
    source - A string to scan

    It's not a filename - it's just a string.

    You should use the Scanner(File) constructor instead - or better yet, the Scanner(File, String) constructor to specify the encoding as well. For example:

    try (Scanner scanner = new Scanner(new File(this.fileName), "UTF_8")) {
        ...
    }
    

    (Note the use of a try-with-resources statement so the scanner gets closed automatically.)

    0 讨论(0)
提交回复
热议问题