为什么input.next()可用于搜索ArrayList,但.nextLine()不起作用?

时间:2015-10-26 16:55:43

标签: java arraylist java.util.scanner

我有一个简单的搜索方法来比较用户的输入String和ArrayList中的String。当我使用input.nextLine()时,它没有找到String,但是,当我使用input.next()时,它工作正常。有人能解释为什么会出现这种错误吗

searchPlants方法是问题发生的地方。

司机:

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

    public class Driver {

public String ID = new String();
public String name = new String();
public String color = new String();
public String smell = new String();
public String hasThorns = new String();
public String isPoisonous = new String();
public String isMedicine = new String();
public String isEdible = new String();

Scanner input = new Scanner(System.in);

public static void main(String[] args) {new Driver();}

public Driver() {

    ArrayList<Plant> plantPack = new ArrayList<>();

    System.out.println("Welcome to the PlantPack Application.");
    System.out.println("Please select a number from the options below.");
    System.out.println("");

    while (true) {

        System.out.println("1: Add a plant to the pack.");
        System.out.println("2: Remove a plant from the pack.");
        System.out.println("3: Search for a specific plant.");
        System.out.println("4: Display your plant pack.");
        System.out.println("5: Filter the plant pack by incomplete name.");
        System.out.println("0: Exit the plant pack interface.");

        // Get the user input
        int userChoice = input.nextInt();

        switch (userChoice) {
            case 1:
                typeSwitch(plantPack);
                break;
            case 2:
                removePlant(plantPack);
                break;
            case 3:
                searchPlants(plantPack);
                break;
            case 4:
                displayPlants(plantPack);
                break;
            case 5:
                filterPlants(plantPack);
                break;
            case 0:
                exitInterface();
                break;
            default:
                System.out.println("Invalid entry. \nPlease choose between 1-5, or 0: ");
                break;
        }
    }
}
public void typeSwitch(ArrayList<Plant> plantPack) {

    System.out.println("Please enter what type of plant you are adding: ");
    System.out.println("1: Flower");
    System.out.println("2: Fungus");
    System.out.println("3: Weed");

    int type = input.nextInt();

    if (type >= 1 && type <= 3) {

        switch (type) {
            case 1:
                addFlower(plantPack);
                break;
            case 2:
                addFungus(plantPack);
                break;
            case 3:
                addWeed(plantPack);
                break;
            }
        }
    else
    {
        System.out.println("Invalid Entry. Please choose between 1-3: ");
        typeSwitch(plantPack);
    }
}
private void addFlower(ArrayList<Plant> plantPack) {

    Flower newFlower = new Flower(ID, name, color, smell, hasThorns, isEdible, isPoisonous, isMedicine);

    if(plantPack.size() < 25)
    {
        System.out.println("Enter a unique ID number for the flower you wish to add (E.g. F1, F2, etc): ");
        newFlower.setID(input.next().trim());
        input.nextLine();                     //Scanner was eating first input for some reason. Had to fire a blank nextLine() to allow input for every entry, as seen in each subsequent method.
        System.out.println("Enter the name of the flower you wish to add: ");
        newFlower.setName(input.nextLine().trim());
        System.out.println("Enter the color of the flower you wish to add: ");
        newFlower.setColor(input.nextLine().trim());
        System.out.println("Does the flower have a scent? Yes or No: ");
        newFlower.setSmell(input.nextLine().trim());
        System.out.println("Does the flower have thorns? Yes or No: ");
        newFlower.setThorns(input.nextLine().trim());
        System.out.println("Flower successfully added.\n ");

        plantPack.add(newFlower);
    }
    else
    {
        System.out.println("You may only hold 25 items in your PlantPack. Please remove an item before adding another.\n");
    }
}
private void addFungus(ArrayList<Plant> plantPack) {
    Fungus newFungus = new Fungus(ID, name, color, smell, hasThorns, isEdible, isPoisonous, isMedicine);//(ID, name, color, isPoisonous);

    if(plantPack.size() < 25)
    {
        System.out.println("Enter a unique ID number for the Fungus you wish to add (E.g. Fn1, Fn2, etc): ");
        newFungus.setID(input.next().trim());
        input.nextLine();
        System.out.println("Enter the name of the Fungus you wish to add: ");
        newFungus.setName(input.nextLine().trim());
        System.out.println("Enter the color of the Fungus you wish to add: ");
        newFungus.setColor(input.nextLine().trim());
        System.out.println("Is this particular Fungus poisonous? Yes or No: ");
        newFungus.setIsPoisonous(input.nextLine().trim());
        System.out.println("Fungus successfully added.\n");

        plantPack.add(newFungus);
    }
    else
    {
        System.out.println("You may only hold 25 items in your plant pack. Please remove one before adding another.");
    }
}
private void addWeed(ArrayList<Plant> plantPack) {
    Weed newWeed = new Weed(ID, name, color, smell, hasThorns, isEdible, isPoisonous, isMedicine);  //(ID, name, color, isEdible, isPoisonous);

    if(plantPack.size() < 25)
    {
        System.out.println("Enter a unique ID number for the Weed you wish to add (E.g. W1, W2, etc): ");
        newWeed.setID(input.next().trim());
        input.nextLine();
        System.out.println("Enter the name of the Weed you wish to add: ");
        newWeed.setName(input.nextLine().trim());
        System.out.println("Enter the color of the Weed you wish to add: ");
        newWeed.setColor(input.nextLine().trim());
        System.out.println("Is this particular Weed edible? Yes or No: ");
        newWeed.setIsEdible(input.nextLine().trim());
        System.out.println("Is this particular Weed medicinal? Yes or No: ");
        newWeed.setIsMedicine(input.nextLine().trim());
        System.out.println("Weed successfully added.\n");

        plantPack.add(newWeed);
    }
    else
    {
        System.out.println("You may only hold 25 items in your plant pack. Please remove one before adding another.");
    }
}
private void removePlant(ArrayList<Plant> plantPack) {

    System.out.println("Which type of Plant do you wish to remove?");
    System.out.println("1: Flower");
    System.out.println("2: Fungus");
    System.out.println("3: Weed");

    int type = input.nextInt();
    switch (type) {

        case 1:
            System.out.println("Enter the ID number of the Flower you want to remove: ");
            String deleteFlowerID = input.next().trim();
            input.nextLine();
            System.out.println("Enter the name of the Flower you wish to remove: ");
            String deleteName = input.nextLine().trim();

            boolean found = false;

            Iterator<Plant> itr = plantPack.iterator();   //a basic for loop and .equals() would not work, and I don't understand why. I had to go with an Iterator.
            while(itr.hasNext()) {
                Plant flowers = itr.next();
                if(flowers.getID().equals(deleteFlowerID) && flowers.getName().equals(deleteName)) {
                    itr.remove();

                    found = true;
                    break;
                }
            }
            if (found)
            {
                System.out.println("That Flower was successfully removed from your inventory.");
            }
            else
            {
                System.out.println("That Flower was not found in your inventory.");
            }
            break;
        case 2:
            System.out.println("Enter the ID number of the Fungus you want to remove: ");
            String deleteFungusID = input.next().trim();
            input.nextLine();
            System.out.println("Enter the name of the Fungus you wish to remove: ");
            String deleteFungusName = input.nextLine().trim();

            boolean foundFungus = false;

            Iterator<Plant> fungusItr = plantPack.iterator();
            while(fungusItr.hasNext()) {
                Plant fungi = fungusItr.next();
                if(fungi.getID().equals(deleteFungusID) && fungi.getName().equals(deleteFungusName)) {
                    fungusItr.remove();

                    foundFungus = true;
                    break;
                }
            }
            if (foundFungus)
            {
                System.out.println("That Fungus was successfully removed from your inventory.");
            }
            else
            {
                System.out.println("That Fungus was not found in your inventory.");
            }
            break;
        case 3:
            System.out.println("Enter the ID number of the Weed you want to remove: ");
            String deleteWeedID = input.next().trim();
            input.nextLine();
            System.out.println("Enter the name of the Weed you wish to remove: ");
            String deleteWeedName = input.nextLine().trim();

            boolean foundWeed = false;

            Iterator<Plant> weedItr = plantPack.iterator();
            while(weedItr.hasNext()) {
                Plant weeds = weedItr.next();
                if(weeds.getID().equals(deleteWeedID) && weeds.getName().equals(deleteWeedName)) {
                    weedItr.remove();
                    foundWeed = true;
                    break;
                }
            }
            if (foundWeed)
            {
                System.out.println("That Weed was successfully removed from your inventory.");
            }
            else
            {
                System.out.println("That Weed was not found in your inventory.");
            }
            break;
    }
}
private void searchPlants(ArrayList<Plant> plantPack) {

    System.out.println("Please enter the ID of the plant you're searching for: ");
    String searchID = input.next().trim();
    input.nextLine();
    boolean match = false;
    for(int i = 0; i < plantPack.size(); i++) {
        if(plantPack.get(i).getID().equals(searchID))
        {
            match = true;
            break;
        }
    }
    if(match)
    {
        System.out.println("Found that one in your PlantPack!");
    }
    else
    {
        System.out.println("Sorry, did not find that one.");
    }
}
private void displayPlants(ArrayList<Plant> plantPack) {

    for(Plant plants : plantPack) {
        System.out.println(plants);
    }
}
private void filterPlants(ArrayList<Plant> plantPack) {

}
private void exitInterface() {
    System.out.println("Are you sure you want to exit the PlantPack Application? Y or N: ");
    while(true) {
        String answer = input.next();
        if(!"Y".equalsIgnoreCase(answer) && !"N".equalsIgnoreCase(answer))
        {
            System.out.println("Please enter Y or N (not case-sensitive): ");
        }
        if("Y".equalsIgnoreCase(answer))
        {
            System.out.println("Thank you for using the PlantPack Application. See ya later!");
            System.exit(0);
        }
        if("N".equalsIgnoreCase(answer))
        {
            break;
        }
    }
}
}

植物类:

    public class Plant {

public String ID;
public String name;
public String color;
public String smell;
public String hasThorns;
public String isEdible;
public String isPoisonous;
public String isMedicine;

public Plant(String ID, String name, String color, String smell, String hasThorns, String isEdible, String isPoisonous, String isMedicine) {

    this.ID = ID;
    this.name = name;
    this.color = color;
    this.smell = smell;
    this.hasThorns = hasThorns;
    this.isEdible = isEdible;
    this.isPoisonous = isPoisonous;
    this.isMedicine = isMedicine;
}

public void setColor(String color) {this.color = color;}
public void setID(String ID) {this.ID = ID;}
public void setName(String name) {this.name = name;}

public String getID() {return ID;}
public String getName() {return name;}
public String getColor() {
    return color;
}

public String toString() {
    return "ID: " + this.ID + ", Name: " + this.name + ", Color " + this.color;
}

}

和花班:

    public class Flower extends Plant {

public Flower(String ID, String name, String color, String smell, String  hasThorns, String isEdible, String isPoisonous, String isMedicine) {

    super(ID, name, color, smell, hasThorns, isEdible, isPoisonous, isMedicine);

   }
    public void setSmell(String smell) {this.smell = smell;}
    public void setThorns(String hasThorns) {
    this.hasThorns = hasThorns;
}
public String getSmell() {
    return smell;
}
public String getHasThorns() {
    return hasThorns;
}
public String toString() {
    return super.toString() + ", Scent? " + this.smell + ", Thorns? " + this.hasThorns;
}
}

这是使用input.nextLine()时的输出:

    Welcome to the PlantPack Application.
    Please select a number from the options below.

    1: Add a plant to the pack.
    2: Remove a plant from the pack.
    3: Search for a specific plant.
    4: Display your plant pack.
    5: Filter the plant pack by incomplete name.
    0: Exit the plant pack interface.
    1
    Please enter what type of plant you are adding: 
    1: Flower
    2: Fungus
    3: Weed
    1
    Enter a unique ID number for the flower you wish to add (E.g. F1, F2, etc): 
    F1
    Enter the name of the flower you wish to add: 
    rose
    Enter the color of the flower you wish to add: 
    red
    Does the flower have a scent? Yes or No: 
    yes
    Does the flower have thorns? Yes or No: 
    yes
    Flower successfully added.

    1: Add a plant to the pack.
    2: Remove a plant from the pack.
    3: Search for a specific plant.
    4: Display your plant pack.
    5: Filter the plant pack by incomplete name.
    0: Exit the plant pack interface.
    4
    ID: F1, Name: rose, Color red, Scent? yes, Thorns? yes
    1: Add a plant to the pack.
    2: Remove a plant from the pack.
    3: Search for a specific plant.
    4: Display your plant pack.
    5: Filter the plant pack by incomplete name.
    0: Exit the plant pack interface.
    3
    Please enter the ID of the plant you're searching for: 
    F1
    Sorry, did not find that one.

现在,同样的事情,但改为使用input.next():

    Please enter the ID of the plant you're searching for: 
    F1
    Found that one in your PlantPack!

1 个答案:

答案 0 :(得分:1)

程序“吃掉”你的新系列。这将导致程序跳过一行。

如果您输入: input.nextLine(); input.nextLine(); (两倍)

它应该有用