Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.util.NoSuchElementException错误,scan.next()问题_Java - Fatal编程技术网

java.util.NoSuchElementException错误,scan.next()问题

java.util.NoSuchElementException错误,scan.next()问题,java,Java,创建一个将文本附加到现有文件的方法,但我一直收到一个错误 请注意,我只粘贴了一个方法,而没有粘贴整个代码 错误: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Roster.Team.add(Team.java:47) at Roster.Main.main(Main.java:22) 代码:

创建一个将文本附加到现有文件的方法,但我一直收到一个错误

请注意,我只粘贴了一个方法,而没有粘贴整个代码

错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Roster.Team.add(Team.java:47)
at Roster.Main.main(Main.java:22)
代码:


出现此错误是因为在扫描仪中找不到行,请添加到代码中

if(scan.hasNextLine()){
System.out.println(“输入要向其中添加信息的文件名:”);
字符串名;
name=scan.nextLine();
试一试{
BufferedWriter writer=new BufferedWriter(new FileWriter(名称,true));
对于(玩家p:x)
如果(p!=null){
writer.write(p+“\n”);
writer.newLine();
}
writer.close();
}捕获(IOE){
//最好在这里记录异常
e、 printStackTrace();
}
}否则{
System.out.println(“扫描仪没有要读取的行”);
}

检查如何在java中使用扫描仪。

您是如何实例化扫描的?您至少应该显示所有相关代码。另外:永远不要隐藏异常。我们的意思是说,
catch
块至少应该包含一个
e.printStackTrace()
。无论如何,不要只发布没有所有相关信息的方法。而不是你的问题和张贴。我肯定你在某处关闭了扫描仪,一旦关闭,就无法打开新的扫描仪。或者类似的,但我们无法从您发布的代码中看出这一点。请使用
系统检查您没有超过一个扫描仪对象。在
中,并检查您的扫描仪对象是否尚未关闭。如果您已按照下面的评论中所述解决了此问题,请发布您找到的解决方案,或者删除不再存在的问题相关。您可以指定如何使用它吗?请检查更新版本。如果hasNextLine返回false,则表示没有任何内容可从该扫描仪读取,如果您尝试,则会导致java.util.NoSuchElementException。这是什么意思?你没有解释为什么没有东西可读。我怎么知道为什么没有东西可读?我解释了你为什么会得到这个例外。如果你想让别人帮你解释为什么你的扫描器里没有什么可读的,请提供你是如何初始化它的代码。可能的原因是您已经到达了文件的末尾。我不理解您的代码,在询问问题之前,扫描仪应该如何得到答案(下一行)?
    public void add(){
    System.out.println("Enter the name of the file that you wish to add information to: ");
    String name;
    name = scan.nextLine();
    try{
       BufferedWriter writer = new BufferedWriter(new FileWriter(name, true));
        for(Player p : x)
            if(p != null){
                writer.write(p + "\n");
                writer.newLine();
            }
        writer.close();
    } catch (IOException e) { }
}