Scanner nextLine() NoSuchElementException

前端 未结 2 631
一个人的身影
一个人的身影 2021-01-23 00:22

I\'ve been programming a Bukkit plugin for a while now, and this one issue has me stumped. I\'m trying to read a line from a file using a Scanner, and add everything on the line

相关标签:
2条回答
  • 2021-01-23 01:02

    in.nextLine() you are calling this twice but checking hasNextLine() only once.

    In second call you might have line available.

    players.add(in.nextLine().split(": ")[0]);

    How to fix?

    String tempLine = in.nextLine();
    

    Then do operations on this string

    0 讨论(0)
  • 2021-01-23 01:06

    try this:

    import java.io.*;
    import java.lang.*;
    import java.util.*;
    public class test {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while(in.hasNextLine()) {
                String line = in.nextLine();
                String parse = line.substring(0, line.lastIndexOf(':'));
                System.out.println(parse);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题