1

What are the alternatives to using the Scanner class for reading input from the console for beginning/intermediate programmers?

I started looking yesterday, and Google took me to some articles about natural language processing and some lists of natural language processing toolkits. The lists I found were not very well annotated, and the topics got very advanced, very quickly.

I ask because I began to wonder about making a custom list of delimiters, such as "+" | "-" | "*" | "/". This doesn't appear to be possible using Scanner.

5
  • Are you planning to create a custom list of delimiters or a custom combination of them? Because Scanner does allow you to specify your own delimiter pattern. But it sounds like you want to delimiters to be context sensitive?
    – Perception
    Dec 14, 2012 at 16:26
  • Not sure what you really want, but you can split a string using regular expressions, indexOf(), tokenizers, etc.
    – PhiLho
    Dec 14, 2012 at 16:26
  • @Perception - Yes, context sensitive, so that if someone inputs a simple math expression, like "3 + 5" or "4 * 2", I could parse it using Scanner (or something else). Dec 14, 2012 at 16:37
  • @PhiLho - That is what I have been doing, but I was wondering if there were something along the line of Google's Guava out there that would do that for me. Dec 14, 2012 at 16:39
  • 2
    Not sure you want to handle this on the scan phase, as you need the delimiters as well as the tokens. Reading in the entire input string and then parsing it would be much better for what your describing.
    – Perception
    Dec 14, 2012 at 16:44

3 Answers 3

4

I began to wonder about making a custom list of delimiters, such as "+" | "-" | "*" | "/". This doesn't appear to be possible using Scanner.

Are you trying to achieve something like this?

String text="Some text +with -delimiters *described /above";
Scanner in=new Scanner(text);
in.useDelimiter("\\+|\\-|\\*|/"); // +,-,* are metacharacters in regex
                                  // to escape its special meaning we need 
                                  // to use "\\" before each of them
int counter=0;
while(in.hasNext())
    System.out.println(counter++ +") "+ in.next());

Output

0) Some text 
1) with 
2) delimiters 
3) described 
4) above
2
  • Yes, that is what I'm trying to do. So you don't have to include anything in the useDelimiter() parameters to indicate that you want it to continue to use whitespace in addition to the ones you listed? Dec 14, 2012 at 16:44
  • 1
    @e.inglewood Not sure what you mean by "continue to use whitespace". To include white-spaces in delimiter you can use \\s before and after it like \\s*(\\+|\\-|\\*|/)\\s* (* means 0 or more elements that are before it)
    – Pshemo
    Dec 14, 2012 at 16:52
2

It sounds like you're writing an interpreter, and want to evaluate expressions you're passed in.

My suggestion would be to look at Flex, which will perform lexical analysis on your input and turn it into tokens. And then take a look at Gnu Bison, which will take that token set, and parse them into an expression tree.

Flex and Bison will effectively create a "mini compiler" inside of your program, and use it to parse input, but this will allow you to handle context sensitive things much better than while using Scanner.

TL;DR

Flex: String -> Tokens

Bison: Tokens -> Expression Tree

You: Expression Tree -> Magic

1
  • Thank you. I will take a look at both of those. Dec 14, 2012 at 16:51
1

You can do it with BufferedReaders and InputStreams.

BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.