Forum Moderators: phranque
I hope this is the right subforum for posting my question.
I need to set up the Java StringTokenizer to read in 4 variables
from a command prompt. It should be in the following format:
>X variable1 variable2 variable3
where
> (is the command prompt)
X (is either +, -, *, /, c, or e)
variable1 variable2 and variable3 are integers.
I will appreciate any answers that will help me go in the right direction.
Welcome to WebmasterWorld!
What are you trying to achieve with the StringTokenizer?
Arguments are accepted by Java programs from the command line through the main(String []) method. The StringTokenizer class takes a single string as an argument and splits it - you seem to want to pass Integers?
Hopefully will be able to help you out with a few more details.
I don't want the program to accept arguments AT the command line. When the program is execute, it should go to a ">" prompt, and allow commands to be entered succively unless "e" is entered. (I know how to implement this).
The program itself is a number base calculator. Two types of instructions can be entered:
c base1 base2 number: instructs the program to convert from the number (an integer) from numberbase1 to numberbase2 (eg >c 10 16 99 would convert from base 10 decimal to base 16 hexdecimal the number 99)
+ (or -, or *, or /) base number1 number 2: performs the arithmatic operation on number1 and number2 in the specified number base (eg >- 10 67 12 would subtract 12 from 67 in base 10).
I don't fully understand how to implement and pass the InputStreamReader to the BufferedReader and then to the StringTokenizer, seperating the 4 components of the string entered.
Thanks for any info!
import java.io.*;
import java.util.*;
public class nbc
{
static public void main(String args[])
{
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
StringTokenizer tokenizer;
// parse the string
StringTokenizer tokenizer = new StringTokenizer(commandline, " ");
ArrayList resList = new ArrayList();
while (tokenizer.hasMoreTokens())
{
resList.add(tokenizer.nextToken());
}
}
}
Well you can pass the InputStreamReader directly to the BufferdReader when you initialise it:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Then store the input from the BufferReader in a String and pass it over to the StringTokenizer, something like:
String text = "";
try{
text = in.readLine();
}
catch(IOException ioe){
ioe.printStackTrace();
}
StringTokenizer tokenizer = new StringTokenizer(text);
ArrayList resList = new ArrayList();
while (tokenizer.hasMoreTokens()){
resList.add(tokenizer.nextToken());
}
Any help?
public static void main(String[] args) means you have an array of Strings passed to you. Are the four arguments passed as just one, in quotes?
String first = args[0];
String second = args[1];
etc...
If for some odd reason you absolutely need to use a StringTokenizer, this would be more reasonnable:
try {
String first = tokenizer.nextToken();
//etc...
}
catch (NoSuchElementException nsee)
{
System.out.println("hey, I need four arguments!");
}
HTH