Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore parentheses while using string tokenizer to read in fractions?

public Fraction (String fractionString)
{
  StringTokenizer st = new StringTokenizer(fractionString, "/");
  numerator = Integer.parseInt(st.nextToken());
  denominator = Integer.parseInt(st.nextToken());
}

I have this so far. How do I change this to ignore parentheses in a fraction?

Example: (3/4) - how do I ignore these parentheses?

Looking at this would I be able to simply do StringTokenizer st = new StringTokenizer(fractionString, "/()"?

like image 204
user3291586 Avatar asked Feb 04 '26 03:02

user3291586


1 Answers

One way to ignore symbols such as ()+-*/ is to use one of the overloaded constructors for StringTokenizer.

StringTokenizer st = new StringTokenizer(fractionString, "()", false);

The boolean for the third argument denotes whether or not the tokenizer will put the given delimeters, in this case parentheses, into tokens themselves (true), or skip over them while tokenizing the rest of the string (false).

like image 106
Jodo1992 Avatar answered Feb 05 '26 20:02

Jodo1992



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!