• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unique Scanner Problem  RSS feed

 
marcus conway
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm have a scanner 'String str' that scans user imput via . The user has to input one of choices from this menu:

I have a series of 'if' and 'else if' statements to decipher the user's input:

...As you can see, I can't figure out how to make the scanner differentiate between a string (get, drop, etc.) and a string with two ints after it(go # #). The user should be able to input 'go # #'(replacing the two #s with integers).

How should I go about doing this? I've been playing around with 'StringTokenizer', but to no avail.

Any help would be greatly appreciated.
Thanks,

n
[ November 06, 2008: Message edited by: marcus conway ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure there's anything "unique" about this problem!

next() just returns one whitespace-separated token; the ints would be the next few tokens, not part of that first one. You can call next() twice more to get each additional token in turn as two more strings; or you can use nextInt() instead, to return the next token as an int (which is most likely what you'll want to do.)
 
marcus conway
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Not sure there's anything "unique" about this problem!

next() just returns one whitespace-separated token; the ints would be the next few tokens, not part of that first one. You can call next() twice more to get each additional token in turn as two more strings; or you can use nextInt() instead, to return the next token as an int (which is most likely what you'll want to do.)


Yeah I know that. But what if the user inputs, lets say, 'drop'? Won't the scanner be waiting for two ints as well?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marcus conway:


Yeah I know that. But what if the user inputs, lets say, 'drop'? Won't the scanner be waiting for two ints as well?


Replace these two lineswithThen you will only try to read in the next two ints if you have found 'go'
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!