0

I have been through trouble working with the Scanner class. My method creates folders and the only required input from the user is the folder's name. However I have to input the data 3 times before getting the program flow through the ifs. The examples of the prompts and the code snipt are pasted below. Would you please help me figure out what's going on?

"==============================Test Folder==============================

Insert the folder name or press q to cancel:

personal

personal

personal

Add new folder or press q to cancel:

work

work

work

Add new folder or press q to cancel:

vacations

vacations

vacations

Add new folder or press q to cancel:

q

Results

personal 1 0

work 2 0

vacations 3 0"

"=========================End of Test Folder=========================

       ArrayList<Folder> folders = new ArrayList<Folder>();

       System.out.println("Insert the folder name or press q to cancel: ");
       Scanner scanner = new Scanner(System.in);

       while(!scanner.nextLine().equals("q")){ 

                if (!Folder.folderExists(scanner.nextLine(), folders)){ //checks if a folder with the same name exists
                    folders.add(new Folder(scanner.nextLine(), Folder.getNextFolderID(folders)));

                    System.out.println("Add new folder or press q to cancel: ");
                }
                else{
                    System.out.println("A folder with the same name already exists. Choose a new folder name or press q to cancel: ");
                }
            }
  • What do you think `nextLine` does? Why do you think so? What does the `Scanner` do with the corresponding input? – Savior Apr 19 '16 at 22:59
  • Hint: The [javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--) is very clear about this. – Savior Apr 19 '16 at 23:05
  • Hi Pilar, thank you very much for your feedback. Jayson pretty much clarified all the issues and the code is working right now. Nonetheless I will definely go over the javadoc and check it everytime from now on before posting questions around here. Best regards :)! – Thao Goldfinger Apr 19 '16 at 23:27

1 Answers1

0

Everytime you call

scanner.nextLine();

you are asking for input from the user. You have one in the condition check in the while loop, one in the if statement, and one in the folders add method call. You can solve this by asking for the input once and storing it, for example:

String userInput = scanner.nextLine();

while(!userInput.trim().equals("q")) { 
    if (!Folder.folderExists(userInput, folders)){ //checks if a folder with the same name exists
        folders.add(new Folder(userInput, Folder.getNextFolderID(folders)));
        System.out.println("Add new folder or press q to cancel: ");
    }
    else{
        System.out.println("A folder with the same name already exists. Choose a new folder name or press q to cancel: ");
    }
    userInput = scanner.nextLine();
}
Jayson
  • 940
  • 8
  • 14
  • Hi Jayson, thank you very much for the reply. I did as you said and the code is working properly now. I didn't know that everytime "nextLine()" is called it also prompts for user inputs. I should have looked at the javadoc as Pillar's suggestion. – Thao Goldfinger Apr 19 '16 at 23:25