簡體   English   中英

為什么input.next()用於搜索ArrayList,而.nextLine()卻不起作用?

[英]Why does input.next() work for searching an ArrayList, but .nextLine() does not?

我有一個簡單的搜索方法,可以將用戶的輸入String與ArrayList中的String進行比較。 當我使用input.nextLine()時,找不到字符串,但是當我使用input.nextLine()時,它起作用了。 有人可以解釋為什么會發生此錯誤嗎?

問題發生的位置是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!

該程序將“占用”您的新行。 這將導致程序跳過一行。

如果鍵入:input.nextLine(); input.nextLine(); (兩次)

它應該工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM