2

This is my first time using tokenizer for a fraction calculator of sorts. I tried following syntax as much as possible, but got this ugly, ugly error:

Fraction.java:78: error: no suitable constructor found for 
StringTokenizer(Fraction,String,boolean)
    StringTokenizer parser = new StringTokenizer(oldMeasure, "/", true);                             
constructor StringTokenizer.StringTokenizer(String) is not applicable
  (actual and formal argument lists differ in length)
constructor StringTokenizer.StringTokenizer(String,String) is not applicable
  (actual and formal argument lists differ in length)
constructor StringTokenizer.StringTokenizer(String,String,boolean) is not applicable
  (actual argument Fraction cannot be converted to String by method invocation conversion)

and here is the code

    public void read(Scanner theKeyboard)
    {
    StringTokenizer parser = new StringTokenizer(oldMeasure, "/");
    if(parser.countTokens() !=2)
        throw new RuntimeException("bad format for fraction");
    myNumerator = Integer.parseInt(parser.nextToken());
    myDenominator = Integer.parseInt(parser.nextToken());
    }
brttwrd
  • 47
  • 1
  • 9
  • 1
    What don't you understand about the error?`no suitable constructor found for StringTokenizer(Fraction,String,boolean)` Do you know what a constructor is? Do you know what the types listed are? – Sotirios Delimanolis Mar 25 '14 at 18:51
  • Also, `(actual argument Fraction cannot be converted to String by method invocation conversion)`. It's giving you all the information. – Sotirios Delimanolis Mar 25 '14 at 18:55
  • what is the value of oldMeasure? – Arjit Mar 25 '14 at 19:02
  • This doesn't answer your question, but from the javadoc for `StringTokenizer`: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead." – Ian McLaird Mar 25 '14 at 20:49

1 Answers1

0

The first parameter you are passing in a constructor must be a String. It is a Fraction, not a String. Find the way to convert it to a String, Java compiler cannot do this for you and little can be advised without knowing about the Fraction type much.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93