0

I wrote a simple entry based program, and the scanner is skipping over it. I have tried various methods that have helped in the past, but to no avail. I appreciate all help.

The below code is all the code in my main method.

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();
arsb48
  • 563
  • 4
  • 10
  • What is the exact problem? How is `scan` defined? Do you want to print out `prgm`? – Scary Wombat Mar 02 '16 at 04:32
  • The output is used in a variety of if loops to call different methods. My scanner is "Scanner scan = new Scanner(System.in);". I do not wish to print prgm, just to see if it matches any of the set responses. – arsb48 Mar 02 '16 at 04:33
  • 2
    Without seeing more code and knowing exactly what the problem is, I'll take a wild guess and say try `scan.next()` instead of `scan.nextLine()` – Jonah Haney Mar 02 '16 at 04:35
  • You should post the entire code so that people are able to tell what is wrong. – WIR3D Mar 02 '16 at 04:36
  • 1
    A string is an object and cannot be compared using `==` you need to use `.equals()`. It took the input, but it did not pass any of the arguments because you were not comparing it properly. This has nothing to do with the scanner. – WIR3D Mar 02 '16 at 04:40
  • Ah, thanks. I'll put it in now. – arsb48 Mar 02 '16 at 04:42

1 Answers1

1

You need to be using:

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

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

Community
  • 1
  • 1
Jonah Haney
  • 521
  • 3
  • 12