在Java中,是否应将不同的Scanner实例用于不同类型的输入? [英] In Java, should I use a different Scanner instance for different types of input?

查看:139
本文介绍了在Java中,是否应将不同的Scanner实例用于不同类型的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从用户那里获得输入.他们开始创造一朵新花.用户告诉我花朵的名称(String),花朵的颜色(String),花朵上的刺数(int)和花朵的气味(String).我使用了一个名为input的扫描仪来获取所有这些信息.但是,它将无法正常工作.得到刺的数目后,该程序将询问用户花朵的气味,但不会让我输入答案.但是,我创建了第二个名为input2的扫描仪来获取刺的数量,现在它可以正常工作.这是代码:

I need to get input from the user. They get to create a new flower. The user tells me the flower's name (String), the flower's color (String), the number of thorns on the flower (int) and the flower's smell (String). I was using a single Scanner named input to get all of this. However, it wouldn't work properly. After it got the number of thorns, the program would ask the user what the flower smells like but it wouldn't let me input an answer. However, I created a second Scanner named input2 for getting the number of thorns and now it works properly. Here is the code:

import java.util.Scanner;
import java.util.ArrayList;

public class AssignmentTwo {

    static ArrayList<FlowerClass> flowerPack = new ArrayList<FlowerClass>();


    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        while(true){
            System.out.println("1. Add flower to flowerpack.");
            System.out.println("2. Remove flower from the flowerpack.");
            System.out.println("3. Search for a flower in the flowerpack.");
            System.out.println("4. Display the flowers in the flowerpack.");

            int userChoice = input.nextInt();

            switch(userChoice){
            case 1:
                addFlower();
                break;
            case 2:
                //removeFlower();
                break;
            case 3:
                //searchFlower();
                break;
            case 4:
                displayFlowers();
                break;
            case 5:
                System.out.println("Goodbye!");
                System.exit(0);
            }
        }
    }

    public static void addFlower(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower's name?");
        String desiredName = input.nextLine();
        System.out.println("What is the flower's color?");
        String desiredColor = input.nextLine();
        System.out.println("How many thorns does it have?");
        Scanner input2 = new Scanner(System.in);
        int desiredThorns = input2.nextInt();
        System.out.println("What does it smell like?");
        String desiredSmell = input.nextLine();
        flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
    }

    public static void displayFlowers(){

        for (FlowerClass flower: flowerPack){
            System.out.println(flower.getName());
        }
        System.out.println("Number of flowers in pack: " + FlowerClass.numberFlowers());

    }
}

如果您查看我的addFlower()函数,您会看到我创建扫描器input2并使用input2获取新花所具有的刺数的int的部分.之前,我使用函数中的第一个Scanner实例来获取荆棘数量的输入.但是,它将无法正常工作.旧功能如下所示:

If you look in my addFlower() function you'll see the part where I create a Scanner input2 and use input2 to get the int for the number of thorns the new flower has. Before, I was using the first Scanner instance in the function to get the input for the number of thorns. However, it wouldn't work properly. The old function looked like this:

public static void addFlower(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower's name?");
        String desiredName = input.nextLine();
        System.out.println("What is the flower's color?");
        String desiredColor = input.nextLine();
        System.out.println("How many thorns does it have?");
        int desiredThorns = input.nextInt();
        System.out.println("What does it smell like?");
        String desiredSmell = input.nextLine();
        flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
    }

该功能的第一个版本不起作用是有原因的吗?我设法通过使用新的扫描仪进行了修复,但我不明白为什么第一个版本不起作用.当您尝试使用单个扫描仪进行不同类型的输入时,扫描仪会感到困惑吗?我正在看《 Java教程》文档中的扫描"教程,但没有找到答案.感谢您的宝贵时间!

Is there a reason why the first version of the function didn't work? I managed to fix it by using a new Scanner but I don't understand why the first version didn't work. Does the Scanner get confused when you try to use a single scanner for different kinds of input? I'm looking at the Scanning tutorial from The Java Tutorials docs and I don't see the answer. Thanks for your time!

推荐答案

我认为挂断来自以下事实:当您在nextInt()之后调用nextLine()时,将计算未在nextInt()中解析的换行符作为该行的末尾,因此扫描仪假定您已经完成.一种缓解方法是始终调用nextLine,但将结果String解析为Int.

I think the hangup comes from the fact that when you call nextLine() after nextInt(), the newline that wasn't parsed in nextInt() is counted as the end of the line, so the scanner assumes you're done already. A way to alleviate that is to always call nextLine, but parse the resultant String as an Int.

在大多数情况下,我认为这样做不是一个好主意,但对您而言,这似乎是有益的.

I don't think it's a good idea to do this in most cases, but in your case it seems beneficial.

public static void addFlower(){
    Scanner input = new Scanner(System.in);
    ...
    int desiredThorns = Integer.parseInt(input.nextLine());
    ...
    flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
}

这篇关于在Java中,是否应将不同的Scanner实例用于不同类型的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆