扫描仪要求输入两次,我正在慢慢失去理智

Scanner asks for input twice and im slowly loosing my mind

我正在尝试从扫描仪获取两个输入(如果需要,可以多次输入)。 问题中的代码位于主要功能的其他部分,但我决定共享所有内容,因为我不知道可能会发生冲突。问题是第一个扫描仪(定时扫描仪)工作正常,但其他两个扫描仪(scanner1)要求我先按回车键一次,然后输入数据并再次按回车键。另外,如果它与拥有两个扫描仪有关,那么我希望不要对 scanner1 进行计时。这不是悲剧,而是一个缺陷,我想修复它。有人可以帮助我吗,因为我 运行 没有想法。

抱歉,如果我有拼写错误或我的语法很糟糕。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class TimedScanner implements Runnable
{
    public static void main(String[] args) throws IOException {
        TimedScanner scanner = new TimedScanner();
        System.out.print("Enter the number of searchagents you want to add in 15 second: ");
        String input = scanner.nextLine(15000);
        if (input == null)
        {
            System.out.println("\nNothing was entered. Continuing...");
        }
        else
        {
            Scanner scanner1 = new Scanner(System.in);
            int number = Integer.parseInt(input);
            File myFile = new File("searchagent_list.txt");
            if (!(myFile.exists())) {
                myFile.createNewFile();
            }
            for (int i = 0; i < number; i++) {
                System.out.println("Press Enter!\nEnter the name of searchagent #"+(i+1));
                String name = scanner1.nextLine();
                System.out.println("Press Enter!\nEnter the adress of searchagent #"+(i+1));
                String adress = scanner1.nextLine();
                FileWriter fWrite = new FileWriter(myFile, true);
                BufferedWriter bWrite = new BufferedWriter(fWrite);
                bWrite.write(name+"##"+adress+"##\n");
                bWrite.close();

            }
            System.out.println("Done");

        }
    }

    private Scanner scanner;
    private StringBuilder buffer;
    private boolean reading;
    private Thread t;

    public TimedScanner()
    {
        scanner = new Scanner(System.in);
        buffer = new StringBuilder();
        reading = false;
        t = new Thread(this);
        t.setDaemon(true);
        t.start();
    }

    public String nextLine(long time)
    {
        reading = true;
        String result = null;
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < time && result == null)
        {
            try
            {
                Thread.sleep(30);
            }
            catch (InterruptedException e)
            {
            }
            synchronized (buffer)
            {
                if (buffer.length() > 0)
                {
                    Scanner temp = new Scanner(buffer.toString());
                    result = temp.nextLine();
                }
            }
        }
        reading = false;
        return result;
    }

    @Override
    public void run()
    {
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            synchronized (buffer)
            {
                if (reading)
                {

                    buffer.append(line);
                    buffer.append("\n");

                }
                else
                {
                    // flush the buffer
                    if (buffer.length() != 0)
                    {
                        buffer.delete(0, buffer.length());
                    }
                }
            }
        }
    }
}```

阅读后的return将解决您的问题:

    @Override
    public void run() {
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            synchronized (buffer) {
                if (reading) {

                    buffer.append(line);
                    buffer.append("\n");

                    return;
                } else {
                    // flush the buffer
                    if (buffer.length() != 0) {
                        buffer.delete(0, buffer.length());
                    }
                }
            }
        }
    }

但是创建多个 Scanner 引用没有任何好处。它只是从 System.in 流读取输入。