1

I want to copy/paste an int from a website, I know it's a very good way, even a non-proper one but i didn't find better

With Java.awt.Robot I go at the good position on page, make 2 clics to select the content, and then make a ctrl+C to add it to the clipboard

But then I have to get back the value which is an int

public static String getAsText() {
        String clipText = "";
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        if ((contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            try {
                clipText = (String) contents.getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException ex) {
                Logger.getLogger(ClipboardTransfert.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ClipboardTransfert.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return clipText;
    }    

This works for String, but not int. I tried with :

  • DataFlavor intFlavor = new DataFlavor(int.class, "Integer"); but Exception at isDataFlavorSupported

  • Change type method to int, also to clipText variable, but not working

  • usual casting methods but not working, it prints the String (to be sure => if i do theString + 1, I get : theString1)

So how to ?

azro
  • 53,056
  • 7
  • 34
  • 70
  • so you are getting the desired number as a String at the moment? – Tom K Feb 23 '17 at 14:31
  • Yes exactly, and Integer.valueOf() does nothing – azro Feb 23 '17 at 14:32
  • try Integer.parseInt(yourString) – Tom K Feb 23 '17 at 14:33
  • @TomK This is equivalent to Integer.valueof() and it enters in "usual casting methods" as I said in my question I've already tried – azro Feb 23 '17 at 14:38
  • 1
    i just copied your code, copied a number into my clipboard, changed the functions return type to int and made the last line return Integer.parseInt(clipText); and its working without any problems. – Tom K Feb 23 '17 at 14:43
  • @TomK what is "this works" for you ? If you System.out.print(value+159) does it really add 159 to your value or concatenate the two ? because for me it concatenates – azro Feb 23 '17 at 14:47
  • i am returning an int from the function, so value+159 results in 160 – Tom K Feb 23 '17 at 14:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136472/discussion-between-azro-and-tom-k). – azro Feb 23 '17 at 14:54
  • Stop .. I don't why but now i works ... I add brackets, i remove them and it's working, so sorry for unuseful post .. – azro Feb 23 '17 at 15:02

1 Answers1

1

I might be misunderstanding your problem, but can't you just parse it? Like this:

int clipboardContent = Integer.parseInt(contents.getTransferData(DataFlavor.stringFlavor));
Tamás Szabó
  • 1,388
  • 11
  • 23
  • This is equivalent to Integer.valueof() and it enters in "usual casting methods" as I said in my question I've already tried – azro Feb 23 '17 at 14:37
  • What do you mean by "it prints the string"? Can you show the code where it prints it? – Tamás Szabó Feb 23 '17 at 14:47