Scanner nextLine()NoSuchElementException

时间:2012-09-05 02:38:31

标签: java minecraft bukkit

我一直在为Bukkit插件编程一段时间,而这一个问题让我很难过。我正在尝试使用扫描仪从文件中读取一行,并将“:”之前的所有内容添加到HashSet中。每次尝试时,我都会收到NoSuchElementException,如下所示:

21:01:01 [SEVERE] Could not pass event PlayerLoginEvent to ServerProtect
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
va:341)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
a:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
ava:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
ava:462)
        at net.minecraft.server.ServerConfigurationManagerAbstract.attemptLogin(
ServerConfigurationManagerAbstract.java:273)
        at net.minecraft.server.NetLoginHandler.d(NetLoginHandler.java:112)
        at net.minecraft.server.NetLoginHandler.c(NetLoginHandler.java:41)
        at net.minecraft.server.DedicatedServerConnectionThread.a(DedicatedServe
rConnectionThread.java:44)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:29)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:578)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
Caused by: java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Unknown Source)
        at com.gmail.thecotlsdragon98.ServerProtect.AltAccounts.CheckForUsedIP(A
ltAccounts.java:49)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
va:339)
        ... 13 more

这是我尝试做上面解释的课程。

package com.gmail.thecotlsdragon98.ServerProtect;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;

public class AltAccounts implements Listener
{
    ServerProtect plugin;
    public AltAccounts(ServerProtect instance){
        plugin = instance;
    }
    File usersFile;
    FileWriter outputFile;
    PrintWriter out;
    Scanner in;
    HashSet<String> players = new HashSet<String>();
    StringBuffer buffer = new StringBuffer();
    @EventHandler
    public void newUsersFile(PluginEnableEvent event) throws IOException{
        if(event.getPlugin() == plugin){
            usersFile = new File("plugins\\ServerProtect\\users.yml");
            if(!usersFile.exists()){
                usersFile.createNewFile();
            }
        }
    }
    @EventHandler
    public void CheckForUsedIP(PlayerLoginEvent event) throws IOException{
        outputFile = new FileWriter(usersFile, true);
        out = new PrintWriter(outputFile);
        in = new Scanner(new FileReader(usersFile));
        String[] nameandIP;
        String name;
        String IP;
        String line = in.nextLine();
        while(in.hasNextLine() && !in.nextLine().isEmpty()){
            players.add(line.split(": ")[0]);
            if(!players.contains(event.getPlayer().getName())){
                out.println(event.getPlayer().getName() + ": " + event.getAddress().getHostAddress());
                out.close();
            }
        }
        if(!in.hasNextLine() || in.nextLine().isEmpty()){
            if(!players.contains(event.getPlayer().getName())){
                out.println(event.getPlayer().getName() + ": " + event.getAddress().getHostAddress());
                out.close();
                players.clear();
            }
        }
        if(!plugin.getConfig().getBoolean("alts.kick-on-login")){
            while(in.hasNextLine()){
                nameandIP = in.nextLine().split(": ");
                name = nameandIP[0];
                IP = nameandIP[1];
                if(event.getAddress().getHostAddress().equals(IP) && !event.getPlayer().getName().equals(name)){
                    for(final Player player : plugin.getServer().getOnlinePlayers()){
                        if(player.hasPermission("serverprotect.alts.notify")){
                            player.sendMessage(ChatColor.RED + event.getPlayer().getName() + " has the same IP as " + name + " (" + event.getAddress().getHostAddress() + ")");
                        }
                    }
                }
            }
        }
        else if(plugin.getConfig().getBoolean("alts.kick-on-login")){
            while(in.hasNextLine()){
                nameandIP = in.nextLine().split(": ");
                name = nameandIP[0];
                IP = nameandIP[1];
                if(event.getAddress().getHostAddress().equalsIgnoreCase(IP)){
                    event.setKickMessage("The IP address you've logged in with is used. Alternate account?");
                    event.setResult(Result.KICK_OTHER);
                }
            }
        }
    }
    public void Disable(PluginDisableEvent event)throws IOException{
        if(event.getPlugin() == plugin){
            in.close();
        }
    }
}

我被告知我两次打电话给.nextLine(),当我询问有关如何修复它的建议时,他只是忽略了我。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

试试这个:

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);
        }
    }
}

答案 1 :(得分:0)

in.nextLine()您正在调用此邮件两次,但仅检查hasNextLine()一次。

在第二次通话中,您可能有线路可用。

players.add(in.nextLine()。split(“:”)[0]);

如何解决?

String tempLine = in.nextLine();

然后对此字符串执行操作