StringTokenizer错误

时间:2014-03-25 18:50:45

标签: java stringtokenizer

这是我第一次使用tokenizer进行各种分数计算器。我尽可能地尝试了下面的语法,但是得到了这个丑陋,丑陋的错误:

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)

这是代码

    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());
    }

1 个答案:

答案 0 :(得分:0)

您在构造函数中传递的第一个参数必须是String。它是Fraction,而不是String。找到将其转换为String的方法,Java编译器无法为您执行此操作,并且在不了解Fraction类型的情况下可以建议很少。