3

I am currently trying to implement a Pokedex which just uses an array list to store Pokemon objects. this is supposed to be a command line interface with certain commands like add, remove, info, help, etc. I was just wondering the best way of finding out which command the user entered. They are also allowed to enter more than one command per line.

it is assumed the user enters the correct number of terms after each command.

for example the sample run might be:

>>>add PIKACHU ELECTRIC 1 These mouse-like creatures are among the most sought-after Pokemon.

>>>info PIKACHU
PIKACHU ELECTRIC 1 These mouse-like creatures are among the most sought-after Pokemon.

>>>add RAICHU ELECTRIC 2 remove PIKACHU

Currently i have a StringTokenizer that goes through to see if the next word is .equal("add") and if it is i call my add() method which i already implemented. I'm not sure how to check for multiple commands per line either.

user1874239
  • 305
  • 2
  • 6
  • 10

2 Answers2

0

For command line interface you can use Apache Commons Command Line Interface

and get best example for the same from here

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
  • Its just a console based program that i can only write in Eclipse. I can't use any third party anything. I'm really not that experienced of a programmer but thank you for answering! – user1874239 Dec 04 '12 at 01:36
  • I think rather then writing your own program for console interaction, you better use API available which is bug free,faster,ease to develop and reliable. – Bhavik Ambani Dec 04 '12 at 01:43
  • @BhavikAmbani: It seems that though you are correct, this is not an option in this case. – Chris Gerken Dec 04 '12 at 01:55
  • But at the back end you can use Apache API, which will be relable. – Bhavik Ambani Dec 04 '12 at 04:32
0

You can use the .nextToken() method to get the next String.

Alternatively, you could use the .split() method as follows:

String[] commands = input.split();
if(commands[0].equals("add"))...
if(commands[1].equals("pikachu"))...

or whatever you want to do. Hope that helps!

awolfe91
  • 1,627
  • 1
  • 11
  • 11