Hello, I'm practicing using ArrayList and I've decided to make a simple program that allows the user to add, remove, search, replace, etc. movie titles using ArrayList. The problem is that, when I try to ADD to the list, the program doesn't even wait for the user to enter a String, it just jumps ahead to the .addMovieTitle method from the parent class and prints something else. I'm unsure why it seems to be going ahead without waiting on the user input. I've read over the code, and in my head at least, it seems to make sense and seems that it should be working properly.
Below I'll add the parent class, and the main class I am using to execute the add, remove, etc. commands.
Also, what I've tried as a fix is to use .next() and .nextLine() in different parts of the code to see if it was perhaps an error due to some whitespace.
Parent Movie Class:
// 07.18.2021 import java.util.ArrayList; public class MovieCommands { ArrayList<String> movieList = new ArrayList<String>(); public void printMovieList() { if(movieList.size()==0) System.out.println("There are no movie titles in this list.\n"); else { System.out.println("Movie List:"); for(int i=0; i<movieList.size(); i++) { System.out.println((i+1) + ". " + movieList.get(i)); } } } public void addMovie(String movieTitle) { movieList.add(movieTitle); System.out.println(movieTitle + " added.\n"); } private int findMovieTitle(String movieTitle) { return movieList.indexOf(movieTitle); } public void movieExists(String movieTitle) { if(movieList.contains(movieTitle)) System.out.println(movieTitle + " found.\n"); else System.out.println(movieTitle + " is not in list.\n"); } public void removeMovieTitle(String movieTitle) { int index = findMovieTitle(movieTitle); if(index != -1) { movieList.remove(index); System.out.println(movieTitle + " removed.\n"); }else System.out.println(movieTitle + " not in list.\n"); } public void replaceMovieTitle(String oldTitle, String newTitle) { if(findMovieTitle(oldTitle) != -1) { removeMovieTitle(oldTitle); addMovie(newTitle); } else System.out.println(oldTitle + " is not in list.\n"); } }
Main:
import java.util.Scanner; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println("Welcome to the movie list maker."); userOptions(); }//end main public static void userOptions() { while(true) { System.out.println("Choose from one of the options below: "); System.out.println("0 - End program."); System.out.println("1 - Add a movie title to the list."); System.out.println("2 - Print the list of movie titles."); System.out.println("3 - Search for a movie title within the list."); System.out.println("4 - Remove a movie title from the list."); System.out.print("5 - Replace a movie title for another.\nEnter choice: "); if(sc.hasNextInt()) { optionExecutions(sc.nextInt()); } else { sc.nextLine(); System.out.println("\n\nInvalid option. Try again."); } } }//end userOptions public static void optionExecutions(int option) { MovieCommands movies = new MovieCommands(); switch(option) { case 0: System.out.print("\nGoodbye."); sc.close(); System.exit(0); break; case 1: System.out.print("\nEnter the movie title you would like to add: "); movies.addMovie(sc.nextLine()); break; case 2: movies.printMovieList(); break; case 3: System.out.println("\nEnter the movie title you would like to search for: "); movies.movieExists(sc.next()); break; case 4: System.out.print("Enter the movie title you would like to remove: "); movies.removeMovieTitle(sc.next()); break; case 5: System.out.print("\nEnter the movie title you would like to replace: "); String oldTitle = sc.next(); System.out.print("\nEnter the movie title you will be replacing with: "); String newTitle = sc.next(); movies.replaceMovieTitle(oldTitle, newTitle); break; } userOptions(); } }//end class
Lastly, as a bonus, if I could receive some advice as to how to improve my code in any way possible, I'd appreciate it. I'm coding a bit every day and would like to keep on improving. Anything helps, thank you.