4

Looked for over an hour and cannot seem to find a way to implement this. I have a stringtokenizer object that is breaking apart a string(a mathematical expression). After reaching a certain point I want to be able to just take what is left of the string after the current position(I want to then use it recursively)

If I do

StringTokenizer sTokenizer = new StringTokenizer(expression);
//operations with some of the tokens here
System.out.println(sTokenizer.nextToken());

it will delimit by spaces.

I tried

sTokenizer.nextToken(null)

but that just throws a null pointer exception.

I know I could just put some random string in the parameter that is unlikely to show up in the expression, but that isn't ideal.

If I were implementing this with substrings

expression.substring(currentposition)

would work, but I need stringtokenizer.

In short, I'm trying to figure out a way to retrieve the remainder of a string(that is being used by stringtokenizer) as a single token.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73

1 Answers1

10

You can use sTokenizer.nextToken(""). It is close to what you are looking for. This returns the rest of the String as a single token including the delimiter. Example:

public static void main(String[] args){
    String expression = "a b c d e";
    StringTokenizer sTokenizer  = new StringTokenizer(expression);
    System.out.println(sTokenizer.nextToken());
    System.out.println(sTokenizer.nextToken(""));
}

Output:

a
 b c d e
gonzo
  • 2,103
  • 1
  • 15
  • 27