0

Recently i've been working with Jflex and i noticed that when i try to construct the Yylex object it only accepts java.io.Reader and java.io.InputStream. How could i do if i just want to build the object by using only a String?, like this:

String myString = "Hello world";
String token;
Yylex scanner = new Yylex(myString);

while ((token = scanner.yylex()) != null) {
     //do something
}

In the system i'm trying to build i want the user to write something and then apply the Yylex method to it. In the models i saw that were similar to my idea the user inputs an String and then it is written into a file from where the Yylex will read.

Is it possible to do that? or i'm misunderstanding something?. Is there any other tool you would reccomend instead of Jflex?

Thanks!

mayhem
  • 39
  • 3
  • 9
  • Check http://stackoverflow.com/questions/837703/how-can-i-get-a-java-io-inputstream-from-a-java-lang-string – SJuan76 Mar 16 '14 at 21:31

1 Answers1

0

The simplest way to do want you want would be to use a StringReader, which creates a reader from a String without any other hoops to jump through. Simply use:

Yylex scanner = new Yylex(new StringReader(myString));
kabb
  • 2,474
  • 2
  • 17
  • 24