java - 从 ArrayList 打印带有空格的字符串不正确

标签 java console-application

我玩这个游戏,你可以收集数百种能量提升,而无需记住它们就能知道它们的作用的唯一方法就是查找它们。因此,我试图让自己成为一个“助手”程序,我可以在其中输入电源名称并获得其效果。

下面是完整的代码,去掉了无用的部分。输出示例如下。如果您查看输出,问题应该相当明显。我可以使用什么方法来解决这个问题,或者我做错了什么?

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

public class ProgMain {

 // // *** ------------*** and any variations indicate a change of importance or use

  public static ArrayList<String> itemlist; // ArrayList of items to cycle through to see if item exists in database.

  static {
     itemlist = new ArrayList<String>();

        itemlist.add("genericitem"); // testing item
        itemlist.add("exit"); // allows for internal program exit
        itemlist.add("debuglist"); // allows to print every item in "itemlist" for debug

        // ***-------------------------------------------------***

        itemlist.add(0, "thisitem hasaspace");

  }

static Scanner console = new Scanner (System.in);

public static void main(String[] args) {

    String item = " " // variable to store name of item

    System.out.println("Enter an item name to check the database.");
    System.out.println("Enter 'exit' to quit the program.");
    System.out.println();

    while(true) {
        System.out.print("Item Name: ");
        item = console.next().toLowerCase().replaceAll("\\s", " ");


        /**
         * ONLY FINDS FIRST WORD, even after removal of spaces.
         */
            //DEBUG
        System.out.print("\n     DEBUG: " + item + "\n");

        if (itemlist.contains(item)) { // cycle through database

            displayItemProperties(item); // find item and print properties
            System.out.println();

        } else { // declares if the item isn't found in database
            System.out.println("That item does not exist.");
            System.out.println();
        }
    }
}

static void displayItemProperties(String item) {

    /**
     * Item names must be in LOWERCASE and have NO SPACES.
     */

    // GENERIC IF STATEMENT
    // Easy copy/paste to add new items

    /*

    if (item.equals("")) {
        System.out.println("");
    }

    */
    // **----------------------------------**

    if (item.equals("genericitem")) {
        System.out.println("A generic item.");
    }

    if (item.equals("debuglist")) {
        for (int i = 0; i < itemlist.size(); i++) {
            System.out.println(itemlist.get(i));
        }
    }

    if (item.equals("exit")) {
        System.out.println("Application Terminating...");
        System.exit(0);
    }

    // ***------------------------------------------------------***

    if (item.equals("thisitem hasaspace")) {
        System.out.println("If a name has a space, it wont show up correctly...");
    }

    // introduce new item declarations here     
}
}

这是不带空格输入内容的示例输出:

Enter an item name to check the database.
Enter 'exit' to quit the program.

Item Name: genericitem

 DEBUG: genericitem
A generic item.

Item Name: exit

 DEBUG: exit
Application Terminating...

这就是使用带有空格的名称时发生的情况:

Enter an item name to check the database.
Enter 'exit' to quit the program.

Item Name: thisitem hasaspace

 DEBUG: thisitem
That item does not exist.

Item Name:

 DEBUG: hasaspace
That item does not exist.

Item Name: exit

 DEBUG: exit
Application Terminating...

最佳答案

Scanner 类默认使用任何空格作为分隔符模式。这包括常规的SPACE。因此,当您调用 Scanner.next() 时,它会提取下一个标记,并且 thisite hasaspace 是 2 个单独的标记,因为空格是默认分隔符。

不要调用 Scanner.next(),而是尝试调用 Scanner.nextLine():

    item = console.nextLine().toLowerCase().replaceAll("\\s", " ");

关于java - 从 ArrayList 打印带有空格的字符串不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916856/

相关文章:

java - 同一张表上的左连接

java - 使用动态实体属性循环文本字段组件

c - Win32 console-only 应用程序,防止 "missing dll"对话框

python - 在运行 Windows 批处理文件、vbscript/wscript 或 Python 控制台脚本时将图像显示为初始屏幕

java - 将JSP连接到Oracle DB的教程?

java - java中检查文件夹权限的问题

java - 我只想显示选定树节点的复选框,但我为树中的每个元素获取单选按钮

linux - 读取条形码并通过 HTTP 将它们发送到 php 脚本的守护进程

c# - 控制台应用程序中的多个参数未正确解析

.net - VB.NET 控制台应用程序中的关闭函数