hi

We are creating a program for class which will take keyboard input and then get the instructions from that basically there could be as many as 4 instructions

currently I am doing the following

System.out.println("Enter Input: ");
humanInput = br.readLine();
		    
StringTokenizer humanInputBrokenUp = new StringTokenizer(humanInput, " ");
if(humanInputBrokenUp.nextToken().toString().equals("exit")){
    	System.out.println("quitting the program");
    	System.exit(0);
} else if (humanInputBrokenUp.nextToken().toString().equals("list")){
}

when it gets to the second else if statement it has moved to the second token..... and I cannot use a different variable for each because I do not know how many elements there will be

HELP please! :)

I see only one "else if" statement. You refer to a "second else if". I'm not 100% sure what you are trying to do. You are breaking a sentence into words, hence the " " delimiter. Correct? There are four instructions. I don't know if that means there are four tokens to look for or four words in the sentence. Often, StringTokenizer is used in a while statement. You have it as an "if" statement. That may be a problem, but again, I'm not 100% clear on what you are trying to accomplish. The examples at the top of StringTokenizer might be useful.

http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html

In addition, this link may give some useful examples.

http://www.devdaily.com/java/edu/pj/pj010006/pj010006.shtml

You can also google "java StringTokenizer example". Please elaborate on what needs to be done and what the problem is.

sorry about not putting it in the (code) tags... and I meant the else if compares to the second item in the tokenizer sorry i am a bit of a newbie when it comes to asking for coding assistance over discussion boards

as for clarification on the instructions the prof will basically do something like this

command [tutorial course section] [course [section [time]]] and each bracket level is optional so he could give just 'command', or could give 'command course section' but not 'command section' and then just to make it even more complicated he could do 'command tutorial course section'

i then need to check to see what the command is, then see if he gave tutorial and go through the first set of [] or if he gave a course and go through the last section of []s printing out the requested information

thanks for any help and assistance! :)

sorry about not putting it in the code tags... and I meant the else if compares to the second item in the tokenizer sorry i am a bit of a newbie when it comes to asking for coding assistance over discussion boards

as for clarification on the instructions the prof will basically do something like this

command [tutorial course section] [course [section [time]]] and each bracket level is optional so he could give just 'command', or could give 'command course section' but not 'command section' and then just to make it even more complicated he could do 'command tutorial course section'

i then need to check to see what the command is, then see if he gave tutorial and go through the first set of [] or if he gave a course and go through the last section of []s printing out the requested information

thanks for any help and assistance! :)

Can you give an exact example of a possible instruction and what you are supposed to do with it? Are you actually looking for the words "command", "tutorial", "course", and "section"? Or do you replace those words with commands (i.e. "grep", "find", "sed", awk", whatever)? Or is the input some type of hybrid?

find English 101 01 tutorial

And are the brackets actually in the instruction? Probably not, but I figured I'd clarify.

brackets are not in the instructions

the 5 options are:

command
ex: quit

command course
ex: list CSC108

command course section
ex: list CSC108 L0101

command course section day(/time)
ex: list CSC108 L0101 M

list tut course section
ex: list tut CSC108 T0101

and no ... no brackets

System.out.println("Enter Input: ");
humanInput = br.readLine();
		    
StringTokenizer humanInputBrokenUp = new StringTokenizer(humanInput, " ");
if(humanInputBrokenUp.nextToken().toString().equals("exit")){
    	System.out.println("quitting the program");
    	System.exit(0);
} else if (humanInputBrokenUp.nextToken().toString().equals("list")){
}

You definitely don't want to call nextToken () twice here. list will be the first token still. Store it in a String (in this case, I called the String firstToken ).

System.out.println("Enter Input: ");
humanInput = br.readLine();
		    
StringTokenizer humanInputBrokenUp = new StringTokenizer(humanInput, " ");

String firstToken = humanInputBrokenUp.nextToken();
if(firstToken.equals("exit")){
    	System.out.println("quitting the program");
    	System.exit(0);
} else if (firstToken.equals("list")){
}

You don't need to convert it to a String with the toString () method. It already is a String. From the documentation:

nextToken

public String nextToken()

    Returns the next token from this string tokenizer.

    Returns:
        the next token from this string tokenizer. 
    Throws:
        NoSuchElementException - if there are no more tokens in this tokenizer's string.

You should also check to make sure it HAS at least one more token, before calling nextToken () here:

String firstToken = humanInputBrokenUp.nextToken();

Use the hasMoreTokens () function to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.