java - 扫描仪跳过条目

标签 java bluej

我编写了一个简单的基于条目的程序,扫描仪正在跳过它。过去我尝试过各种有用的方法,但没有效果。我感谢所有帮助。

下面的代码是我的main方法中的所有代码。

Scanner scan = new Scanner(System.in);

    ArrayList <String> list = new ArrayList <String> ( );



    System.out.println("You can choose any of the following:");
    System.out.println("\n  - Add: Add an element to the Array List.");
    System.out.println("\n  - Numbered Remove: Remove an element by it's placement in the Array List.");
    System.out.println("\n  - Named Remove: Remove an element by name.");
    System.out.println("\n  - Clear: Clear the entire Array List");
    System.out.println("\n  - Search: Find a specific element, and the amount of occurences in the Array List.");
    System.out.println("\n  - Find Size: Prints the size of each element in the Array List.");
    System.out.println("\n  - Print: Prints the entire Array List.");
    System.out.println("\n  - Quit: Quits the program.");

    System.out.println();
    System.out.println();

    System.out.println("Type the FIRST word of each listing EXACTLY in order to continue.");
    System.out.print("-->  ");
    String program = scan.nextLine();

    String prgm = program.toLowerCase();

    if (prgm == "add")
    {
        add(list);
    }


    if (prgm == "numbered")
    {
        numbered(list);
    }


    if (prgm == "named")
    {
        named(list);
    }


    if (prgm == "clear")
    {
        clear(list);
    }


    if (prgm == "search")
    {
        search(list);
    }


    if (prgm == "find")
    {
        find(list);
    }


    if (prgm == "print")
    {
        print(list);
    }


    if (prgm == "quit")
    {
        System.out.println("Goodbye.");
    }



    String prgm = program.toLowerCase();

最佳答案

您需要使用:

if (prgm.equals("add")){
    ....
}
....

参见What's the difference between ".equals" and "=="?

关于java - 扫描仪跳过条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35738529/

相关文章:

java - 放在矩阵上的两个非攻击车的最大平方和是多少?

java - 使用 Z3 的 Java 中的段错误

java - 最大和最小程序的数组索引越界异常

java - HashMap 到 txt 文件 - 在 Java OOP 中写入键和值

java - 在Java中调用多维数组的更好方法是什么?

java - 每天同时使所有 Activity session 过期

java - Swagger Spring API - xmlModelPlugin 错误

java - 当调用新 Intent 上的 startActivity 时,DialogFragment 正在重新启动

bluej - 键入时在 BlueJ 中显示代码自动完成建议

java - 快速排序数字(字符串)