简体   繁体   中英

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. After I run the code, I open my MS Word, the app is put in 1 minute delay. Then, the code is supposed to print all the names from my file into MS Word.

But what my code is doing instead, it is just printing the last name in my txt file three times. Now I have printed the names in my IDE with System.out.println() and all of the names are being printed. The problem is when the robot is typing the names in 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

  Ned Stark
  Arya Stark
  Robb Stark

and intead of typing all these names, the followig is being typed

   @Robb Stark
   @Robb Stark
   @Robb Stark

Adding another 1 second delay inside the while loop will solve your problem.

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).

You can use code like this (remember to 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 ?
  • You should use hasNextLine() instead of hasNext() (there's a hasNext…() method for every next…() method, and you should match them up). hasNext() checks for the presence of the next token, not the presence of the next line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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