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: ");
}
}