I have an input that looks like: (0 0 0)
I would like to ignore the parenthesis and only add the numbers, in this case 0, to an arraylist.
I am using scanner to read from a file and this is what I have so far
transitionInput = data.nextLine();
st = new StringTokenizer(transitionInput,"()", true);
while (st.hasMoreTokens())
{
transition.add(st.nextToken(","));
}
However, the output looks like this [(0 0 0)]
I would like to ignore the parentheses
You are first using
()as delimiters, then switching to,, but you are switching before extracting the first token (the text between parentheses).What you probably intended to do is this:
This code assumes that the expression always starts and ends with parentheses. If this is the case, you may as well remove them manually using
String.substring(). Also, you may want to consider usingString.split()to do the actual splitting:Note that both examples assume that commas are used as separators, as in your sample code (although the text of your question says otherwise)