简体   繁体   English

使用 ArrayList 创建选项菜单以添加字符串对象。 当输入包含空格“hello there”时,我得到 java.util.InputMistmachException

[英]Creating an option menu using an ArrayList to add objects of string. When input contains space "hello there" I get java.util.InputMistmachException

`System.out.println();
        ArrayList<String> runescape = new ArrayList<String>();

        while (true) {
            display();

            int optionNum = input.nextInt();
            
            if (optionNum == 1) {
                System.out.print("Enter a String : ");
                runescape.add(input.next());
                System.out.println("Element Added");
            } 
            else if (optionNum == 2) {
                
                System.out.print("Enter String to remove :");
                String remov = input.next();
                if (runescape.contains(remov)) {
                    runescape.remove(new String(remov));
                    System.out.print("The String been removed.");
                }else
                    System.out.println("That String does not exist");
            } else if (optionNum == 3) {
                System.out.print("Your list: " + runescape);
            } else if (optionNum == 4) {
                System.out.print("You have exited the program.");
                break;
            }

        }`

I am wondering why I get this error, if I try entering a String made up of multiple words.我想知道为什么我会收到此错误,如果我尝试输入由多个单词组成的字符串。 For example if I enter "Hello Peter".例如,如果我输入“Hello Peter”。

Thank you for the help in advance.提前感谢您的帮助。

As stated by maloomeister, Scanner.next() will only read a single word (unless you have a different delimiter).正如 maloomeister 所述, Scanner.next()只会读取一个单词(除非您有不同的分隔符)。 To read a full line (all words), use Scanner.nextLine() :要阅读整行(所有单词),请使用Scanner.nextLine()

runescape.add(input.nextLine());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查ArrayList是否包含字符串。 如果不是,则添加该字符串 - Check through an ArrayList if it contains a String. If it doesn't, add that String 如何获取并拆分按空格输入的多个字符串行,然后将它们添加到 Java 中的 arrayList? - how to get and split multiple String lines input by space then add them to arrayList in Java? 使用Castor,如何将Java类“ java.util.ArrayList”映射到元素“ ArrayList”,又如何为其包含的对象生成元素? - Using Castor, how do you map Java class “java.util.ArrayList” to element “ArrayList”, but also generate elements for the objects that it contains? 当我在 Java 中读取 API 时,我在字符串中使用 ÃŽ 代替 Î。 我该怎么办? - When I am reading a API in java I get ÃŽ in place of Î in a string. What should I do? 如何在java中将ArrayList转换为String [],Arraylist包含VO对象 - How to convert ArrayList to String[] in java, Arraylist contains VO objects 如何在Java中将类型为String的用户输入添加到ArrayList? - How do I add a User Input of type String to an ArrayList in Java? Java 在创建大量对象的 ArrayList 时出现堆空间错误? - Java heap space error while creating ArrayList of large number of objects? 如何使用Java ArrayList获得字符串用户输入并通过+1增加值? - How do I get a string user input and increase value by +1 using Java ArrayList? 当 arraylist 包含对象时,如何使用 Junit/Java 测试 compareTo? - How can I test a compareTo with Junit/Java when the arraylist contains objects? 使用用户输入将对象添加到ArrayList的方法 - method to add objects to ArrayList using user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM