-1

I made a calculator and now improving it so that it reads one line of code and turns it into three variables a: first number, b: second number, and function: what it does example: 10 * 10. This is my code:

System.out.println("problem: ");
problem = user_input.next();
StringTokenizer token = new StringTokenizer(problem," ");
a = Integer.parseInt(token.nextToken());
String Menu = (token.nextToken());
b = Integer.parseInt(token.nextToken());

It doesn't understand nextToken at all, it says at Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at mycalc.main(mycalc.java:20), also I asked a friend that actually showed me how this works and he was confused too. Please help me any possible way you can!

TheLittleCoder
  • 21
  • 2
  • 11
  • `Unknown Source` means there isn't any debug information. – Peter Lawrey Apr 17 '16 at 19:19
  • Also http://stackoverflow.com/q/23220224/1743880 and http://stackoverflow.com/q/20104942/1743880 and http://stackoverflow.com/a/10147003/1743880 and http://stackoverflow.com/q/20189486/1743880. Ha and http://stackoverflow.com/q/23968923/1743880... – Tunaki Apr 17 '16 at 19:23

1 Answers1

0

When you do

problem = user_input.next();

this read just one word. e.g. if you input 10 * 10, then this word will be 10

So when you do

String Menu = (token.nextToken());

there is no such element as the error suggests.

Note: you are parsing for words two different ways. It would be simpler to just use the scanner.

System.out.println("problem: ");
a = user_input.nextInt();
String menu = user_input.next();
b = user_input.nextInt();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Isn't StringTokenizer supposed to separate it into several groups between spaces? – TheLittleCoder Apr 17 '16 at 19:25
  • @TheLittleCoder it does. however your input stream only has one word as `next()` only returns one word. – Peter Lawrey Apr 17 '16 at 19:27
  • 1
    It is discouraged to answer duplicate questions as per the FAQ http://meta.stackexchange.com/q/10841. One should vote to close instead. – Tunaki Apr 17 '16 at 19:35
  • 1
    @Tunaki thank you for commenting. Though the duplicate doesn't explain why StringTokenizer shouldn't be used. – Peter Lawrey Apr 17 '16 at 19:45
  • That would be a different question indeed. Which is answered here http://stackoverflow.com/questions/6983856/why-is-stringtokenizer-deprecated?rq=1. – Tunaki Apr 17 '16 at 19:47
  • 1
    @Tunaki yes, although not that one. It talks about using split which wouldn't make sense in combination with Scanner when you want to split on space which is what Scanner does by default. – Peter Lawrey Apr 17 '16 at 19:49
  • The point is, it would be a different question. The question here is solving the exception, which the duplicate (and the linked questions [here](http://stackoverflow.com/questions/36681062/why-doesnt-stringtokenizer-understand-the-nexttoken-function/36681128?noredirect=1#comment60952027_36681062)) answers. One should always search for duplicates before answering. – Tunaki Apr 17 '16 at 19:54
  • 1
    @Tunaki That is shallow interpretation of "Please help me any possible way you can" but we can agree to disagree. – Peter Lawrey Apr 17 '16 at 19:56