mercredi 31 janvier 2018

Java, the clipboard code does not work,

The program should analyze the clipboard for the presence in it of a 5-digit number starting with one. The problem is that when copying the text I do not answer if (clipboardContent.length () == 5) the program stops working.

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;

public class drob implements FlavorListener {
    private static Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    public static void main(String[] args) throws InterruptedException {
        clipboard.addFlavorListener(new drob());

 //  fall asleep for 100 seconds, otherwise the program will immediately end

        Thread.sleep(100 * 1000);
    }

    @Override
    public void flavorsChanged(FlavorEvent event) {
        try {
            String clipboardContent = (String) clipboard.getData(DataFlavor.stringFlavor);
            handleClipboardContent(clipboardContent);
        } catch (UnsupportedFlavorException | IOException e) {
            // TODO handle the error

            e.printStackTrace();
        }
    }

    private void handleClipboardContent(String clipboardContent) {
        // check if the string satisfies condition
           

 // for example, check that the length of the string is five

        if (clipboardContent.length() == 5) {
            System.out.println(clipboardContent);

        }
    }
}

Aucun commentaire:

Enregistrer un commentaire