Tuesday 9 April 2019

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). 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 flowerPack = new ArrayList();


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


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!

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...