简体   繁体   English

在Java中,我应该为不同类型的输入使用不同的Scanner实例吗?

[英]In Java, should I use a different Scanner instance for different types of input?

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). 用户告诉我花的名字(String),花的颜色(String),花上的刺数(int)和花的气味(String)。 I was using a single Scanner named input to get all of this. 我使用单个Scanner命名输入来获取所有这些。 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. 但是,我创建了第二个名为input2的Scanner来获取荆棘数,现在它正常工作。 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());

    }
}

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. 如果您查看我的addFlower()函数,您将看到我创建Scanner input2的部分,并使用input2获取新花所具有的荆棘数的int。 Before, I was using the first Scanner instance in the function to get the input for the number of thorns. 之前,我正在使用函数中的第一个Scanner实例来获取荆棘数的输入。 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));
    }

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. 我正在查看The Java Tutorials docs中的Scanning教程,但我没有看到答案。 Thanks for your time! 谢谢你的时间!

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. 我认为挂断来自这样一个事实:当你在nextInt()之后调用nextLine()时,未在nextInt()中解析的换行被计为行的结尾,因此扫描程序假定你已经完成了。 A way to alleviate that is to always call nextLine, but parse the resultant String as an Int. 缓解这种情况的一种方法是始终调用nextLine,但将结果String解析为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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM