Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Program skipping ahead and not allowing/reading user input

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Program skipping ahead and not allowing/reading user input

    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.
    Last edited by HyperRei; July 19th, 2021 at 01:57 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program skipping ahead and not allowing/reading user input

    The problem is probably due to the way the Scanner class's nextLine method and next... methods work.
    It is explained here: https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Program skipping ahead and not allowing/reading user input

    Thank you, I managed to figure out what was wrong with the link you attached. Thanks again.

Similar Threads

  1. Replies: 3
    Last Post: April 19th, 2014, 03:47 PM
  2. Trouble calculating a product and allowing a user to specify a range
    By jetset22 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 5th, 2013, 01:58 PM
  3. Dialog box not allowing user input?
    By michaelgilbert in forum AWT / Java Swing
    Replies: 13
    Last Post: November 1st, 2011, 05:22 PM
  4. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM

Tags for this Thread