Using Java.util.scanner with GWT

若如初见. 提交于 2019-12-24 00:38:22

问题


for some reason when I try to use scanner with gwt, i get the following error:

No source code is available for type java.util.Scanner; did you forget to inherit a required module?

I looked around and it seems the "No source code is available for type xxxx" errors are due to not having a Javascript equivalent type for the Java type. Is scanner not able to be used with GWT?

Here is a snippet of my code:

import java.util.Scanner;
...
public void submit(){
    String text = editor.getEditor().getText();
    Scanner input = new Scanner(text);
    while(input.hasNextLine()){
        String line = input.nextLine();
        if(line.contains("//")){
            cInfo.setDone(false);
            cInfo.setCode(text);
            return;
        }
        cInfo.setDone(true);
        cInfo.setCode(text);
        }
    }

}

回答1:


java.util.Scanner is not part of the GWT JRE Emulation. If you need a detail overview of what is inside the emulation here is the link to the docs:

https://developers.google.com/web-toolkit/doc/latest/RefJreEmulation#Package_java_util




回答2:


Your code (at least the one in the current version of your question) is probably[*] equivalent to

public void submit() {
  String text = editor.getEditor().getText();

  if ("".equals(text))
    return;

  cInfo.setDone(!text.contains("//"));
  cInfo.setCode(text);
}

However, I have a feeling that this may not actually be what want to do (or is it?)

If you need to split strings on the client side, I usually recommend the Splitter class in Guava. Most of its methods are GwtCompatible, and (together with CharMatcher, Joiner, ...) it's great to use both on the client and server side of your Java code.

[*] assuming, that setDone and setCode are simple setters without side effects



来源:https://stackoverflow.com/questions/11548068/using-java-util-scanner-with-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!