In Java, should I use a different scanner instance for different input types?

I need to get information from the user. They can create a new flower. The user tells me the name of the flower (String), the color of the flower (String), the number of spikes on the flower (int), and the smell of flowers (String). I used one instance of Scanner named input to get it all. However, this will not work properly. After he received the number of spikes, the program will ask the user that the flower smells, but this did not allow me to enter an answer. However, I created a second scanner called input2 to get the number of turns, and now it works correctly. 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 name?");
        String desiredName = input.nextLine();
        System.out.println("What is the flower 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(), , Scanner2, input2, int , . , . . :

public static void addFlower(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower name?");
        String desiredName = input.nextLine();
        System.out.println("What is the flower 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 Tutorials, . !

+4
1

, - , nextLine() nextInt(), , nextInt(), , , . , nextLine, Int.

, , .

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

Source: https://habr.com/ru/post/1545440/


All Articles