简体   繁体   English

机械手一次又一次地打印相同的字符串

[英]Robot is printing the same string over and over again

So I am writing a code where I have a txt file and I have three names in three separate lines in it. 因此,我正在编写代码,其中有一个txt文件,并且在其中的三个单独行中有三个名称。 After I run the code, I open my MS Word, the app is put in 1 minute delay. 运行代码后,我打开MS Word,该应用程序延迟了1分钟。 Then, the code is supposed to print all the names from my file into MS Word. 然后,该代码应该将我文件中的所有名称打印到MS Word中。

But what my code is doing instead, it is just printing the last name in my txt file three times. 但是我的代码在做什么,它只是将txt文件中的姓氏打印了三次。 Now I have printed the names in my IDE with System.out.println() and all of the names are being printed. 现在,我已经使用System.out.println()在我的IDE中打印了名称,并且所有名称都已被打印。 The problem is when the robot is typing the names in MS Word. 问题是当机器人在MS Word中键入名称时。 Can anyone resolve the issue please? 谁能解决这个问题?

   Scanner sc = new Scanner(br);
        String scan = sc.nextLine();
        TimeUnit.MINUTES.sleep(1);
        while (scan != null) {
            System.out.println("I am here");
            System.out.println(scan);
            scan = "@" + scan;
            StringSelection sl = new StringSelection(scan);
            Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
            cb.setContents(sl, sl);

            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);

            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

            robot.keyPress(KeyEvent.VK_SPACE);
            robot.keyRelease(KeyEvent.VK_SPACE);


            if (sc.hasNext()) {
                scan = sc.nextLine();
            } else {
                break;
            }
        }

So my txt file is like this 所以我的txt文件是这样的

  Ned Stark
  Arya Stark
  Robb Stark

and intead of typing all these names, the followig is being typed 并键入所有这些名称,则键入了Followig

   @Robb Stark
   @Robb Stark
   @Robb Stark

Adding another 1 second delay inside the while loop will solve your problem. while循环内添加另一个1秒的延迟将解决您的问题。

TimeUnit.SECONDS.sleep(1);

Here's the code. 这是代码。 I've done some modifications. 我做了一些修改。

Scanner sc = new Scanner(new File("D:\\f.txt"));
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Robot robot = new Robot();

TimeUnit.SECONDS.sleep(2); // Increase the initial sleep time if necessary.

while (sc.hasNextLine()) { // use hasNextLine() so you can get rid of the if block at the end of the code

    TimeUnit.SECONDS.sleep(1); // added another 1 second sleep
    String scan = sc.nextLine();
    scan = "@" + scan;
    System.out.println(scan);
    StringSelection sl = new StringSelection(scan);

    cb.setContents(sl, sl);

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

    robot.keyPress(KeyEvent.VK_SPACE);
    robot.keyRelease(KeyEvent.VK_SPACE);
}

在此处输入图片说明

You probably aren't waiting long enough for the paste to complete. 您可能等待的时间不够长,无法完成粘贴。 You paste really quickly three times. 您很快就粘贴了三遍。 If it takes a while to begin pasting, it might start pasting after you put the last thing in the clipboard. 如果需要一段时间才能开始粘贴,则将最后一件东西放入剪贴板可能会开始粘贴。

If that's the case, an easy solution is to fill the clipboard with all the lines and then to paste that (this assumes that the Scanner input isn't huge, filling up all your memory). 在这种情况下,一个简单的解决方案是在剪贴板中填满所有行,然后粘贴(假设“ Scanner输入量不大,将占用所有内存)。

You can use code like this (remember to import java.awt.datatransfer.StringSelection ): 您可以使用以下代码(记住要import java.awt.datatransfer.StringSelection ):

TimeUnit.MINUTES.sleep(1);

final Scanner scanner = new Scanner(br);
final StringBuilder sb = new StringBuilder();
while (scanner.hasNextLine()) {
    sb.append('@');
    sb.append(scanner.getLine());
    sb.append('\n');
    sb.append(' ');
}

final StringSelection text = new StringSelection(sb.toString());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(text, text);

final Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

A few minor comments on your code, ignoring the potential solution above: 关于您的代码的一些小注释,忽略了上面的潜在解决方案:

  • If you don't change anything else, your loop condition should probably just be true , since nextLine() never returns null (it uses exceptions instead), so how will scan ever be null ? 如果您不进行任何其他更改,则循环条件可能应该为true ,因为nextLine()永远不会返回null (而是使用异常),那么scan怎么会永远为null
  • You should use hasNextLine() instead of hasNext() (there's a hasNext…() method for every next…() method, and you should match them up). 您应该使用hasNextLine()而不是hasNext() (每个next…()方法都有一个hasNext…() next…()方法,并且应该将它们匹配)。 hasNext() checks for the presence of the next token, not the presence of the next line. hasNext()检查下一个标记的存在,而不是下一行的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM