【问题标题】:Java Scanner getting cleared no matter what, how to prevent that?Java Scanner 无论如何都会被清除,如何防止这种情况发生?
【发布时间】:2019-02-11 15:38:47
【问题描述】:

我正在学习 Java,并且正在制作一个库。我想在同一个Scanner上使用三种方法,但是每次都会清空scanner。

我们在课堂上使用 Jcreator,但我的老师也无法弄清楚发生了什么。唯一有效的是

public static void main(String[] args)
{
    Scanner kb = new Scanner(System.in);
    String typedStuff = kb.nextLine();
    Scanner chopper = new Scanner(typedStuff);

    System.out.println(howMany(chopper));
    System.out.println(howManyInts(chopper));
    System.out.println(howManyIntsAndDoubles(chopper));
} 

public static int howMany(Scanner chopper) //
{
    String x = "";
    int y = 0;
    while(chopper.hasNext())
    {
        y++;
        x = chopper.next();
    }
    return y;
}

    public static int howManyInts(Scanner chopper)
        {
            String x = "";
    int y = 0;
    while(chopper.hasNext())
    {
        if (chopper.hasNextInt())
        {
            y++;
        }
        x = chopper.next();
    }
    return y;
}

public static int howManyIntsAndDoubles(Scanner chopper)
{
    String x = "";
    int y = 0;
    while(chopper.hasNext())
    {
        if (chopper.hasNextDouble())
        {
            y++;
        }
        x = chopper.next();
    }
    return y;
}

如果我输入“yes 5.2 2 5.7 6 no”,那么我的输出是: 6 0 0

但它应该是: 6 2 4

我知道它会在第一个方法运行后清除扫描仪,无论它的顺序如何。即使我在方法的第一行将扫描仪转换为另一种数据类型,它仍然会清除原始数据类型。谢谢!

【问题讨论】:

  • 这是因为您正在迭代第一个方法中的所有输入,所以当涉及到第二个函数时,扫描仪没有什么可以扫描的了。
  • 用扫描仪来做这件事很奇怪。使用字符串会更容易。并使用 stringobject.split(" ") 方法(它将您的输入字符串拆分为一个数组。每个数组条目都包含在“空格符号”的拆分部分。然后您可以轻松地使用数组和 for 或while 循环。
  • 如果你不明白,我可以发布正确的答案。嗯

标签: java methods java.util.scanner


【解决方案1】:

我不确定你的 chopper 类是做什么的,但我认为它将输入字符串拆分为空格。如果是这种情况,您可能会在第一个方法 howMany() 中通过调用 chopper.Next() 索引到斩波器的末尾,直到它位于输入的末尾。如果您已经指向斩波器的末端,则在另一个方法中对 chopper.Next() 的下一次调用将为 null。

我会推荐以下内容:

public static String howMany(Scanner chopper){
    String x = "";
    int y = 0;
    int doubleCount=0;
    int intCount =0;
    while(chopper.hasNext()){
        y++;
        if (chopper.hasNextDouble()){
            doubleCount++;
        }
        if(chopper.hasNextInt()){
            intCount++;
        }
        x = chopper.next();
    }
    return x+y+" "+ intCount + " " + doubleCount;
}

【讨论】:

    【解决方案2】:

    我假设这是一项学术练习,您必须使用 Scanner 解决您的问题,并且每次计数一个方法。 您的代码中的问题是您为每个方法使用/传递相同的扫描仪,但是在方法 howMany (第一次调用)中,代码消耗了您输入的所有标记。由于您无法将扫描仪重新设置为从输入的开头重新开始,因此可以声明三个扫描仪(再次,我假设这是一个学术练习,您必须使用扫描仪解决它)并传递每个扫描仪到你的方法。 提示:如果不想使用 chopper.next() 的结果不需要赋值给变量 x,直接调用 chopper.next() 即可。

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        String typedStuff = kb.nextLine();
        Scanner chopperHowMany = new Scanner(typedStuff);
        Scanner chopperHowManyInts = new Scanner(typedStuff);
        Scanner chopperHowManyDoubles = new Scanner(typedStuff);
    
        System.out.println(howMany(chopperHowMany));
        System.out.println(howManyInts(chopperHowManyInts));
        System.out.println(howManyIntsAndDoubles(chopperHowManyDoubles.reset()));
    }
    
    public static int howMany(Scanner chopper) //
    {
        int y = 0;
        while (chopper.hasNext()) {
            y++;
            chopper.next();
        }
        return y;
    }
    
    public static int howManyInts(Scanner chopper) {
        int y = 0;
        while (chopper.hasNext()) {
            if (chopper.hasNextInt()) {
                y++;
            }
            chopper.next();
        }
        return y;
    }
    
    public static int howManyIntsAndDoubles(Scanner chopper) {
        int y = 0;
        while (chopper.hasNext()) {
            if (chopper.hasNextDouble()) {
                y++;
            }
            chopper.next();
        }
        return y;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      相关资源
      最近更新 更多