Search code examples
javastringclipboardawtrobot

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

Solution

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

    enter image description here