当前位置: 首页 > 知识库问答 >
问题:

为什么input.next()可以搜索ArrayList,而.nextLine()却不行?

东门航
2023-03-14
    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;
}
}
    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.
    Please enter the ID of the plant you're searching for: 
    F1
    Found that one in your PlantPack!

共有1个答案

贲骏喆
2023-03-14

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

如果键入:input.nextline();input.nextline();(两倍)

应该管用

 类似资料:
  • 好的,我正在使用gradle编译4个源集,一个是main,另外3个是反射加载的其他小段代码,这些代码基于稍后在“服务器”中可用的其他类。

  • 我想通过一个包含EditText和按钮的水平LinearLayout创建一个自定义SearchView。用户在EditText中输入搜索查询。 通常,当我们使用SearchView时,我们 > 通过将可搜索活动配置为在清单意向来声明该活动。 通过使用清单中的meta元素来声明我们的可搜索配置文件。 通过配置 SearchView 启用系统辅助搜索(其中系统将搜索查询传递给可搜索活动、提供语音搜索、

  • 问题内容: 我不明白为什么主要方法必须是静态的。我了解静态变量,但静态方法很难掌握。是否存在静态方法,以便人们可以在两个不会相互冲突的类中创建两个具有相同名称的方法? 另外,我不明白为什么我不能创建静态构造函数。 谁能帮助解释这个概念? 问题答案: Java有 [静态构造函数] 静态初始化块,可以将其视为“静态构造函数”: 无论如何,主类中唯一 必须 是静态的方法是方法。这是因为调用它之前 没有

  • 问题内容: 我不确定他们已经进行了多长时间,但是我只是注意到Google 在他们的搜索网址中使用 # 而不是 搜索? 。 新方法 http://www.google.com/#q=stackoverflow 旧方法 http://www.google.com/search?q=stackoverflow 井号/井号通常用作页面部分的锚点。 除了较短的网址,对Google有什么好处?它似乎违背了搜索

  • 问题内容: 我希望能够以这种方式一个接一个地获取句子的POS标签: 但是问题是每个句子大约需要一秒钟。还有另一种选择可用于批量执行此操作并加快处理速度。但是,如果我能逐句地做这件事,我的生活会更轻松。 有没有办法更快地做到这一点? 问题答案: 对于NLTK 3.1版,里面,是这样定义的: 因此,每次对first的调用实例化都会花费一些时间,因为它涉及加载pickle文件。 只需调用when是。因此