1
\$\begingroup\$

first time posting here, as well as being new to coding in Java. I am trying to create a movie queue with an array that is able to contain 100 elements, but will only read and print the elements the user enters. I got the first section where a user enters a movie and it stores it and prints it working properly, I am just having problems getting my code to replace a movie that is entered. Here is what I have so far. The issue I am having is within case 2. Thanks for your time.

public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String [] list = new String [100];
        int count = -1;
        boolean cont = true;
        while (cont){
        System.out.println( "Please make a selection:\n" +
                            "1 - Add a new movie\n" +
                            "2 - Update a movie\n" +
                            "3 - Print entire collection\n" +
                            "4 - Quit");
    int response = input.nextInt(); 
    String dummy = input.nextLine();
        switch (response){
            case 1: 
                System.out.print("You have chosen to add a movie\n");
                System.out.print("Please enter a title:\n");
                String title = input.nextLine();
                count++;
                list[count]=title;
                String [] newlist = new String [count+1];    
                newlist[count] = list[count];         
                break;
            
            case 2:
                System.out.println("You have chosen to update a movie");
                System.out.println("Please enter a title:");
                String original = input.nextLine();
                System.out.println("Please enter a new title:");
                String newtitle = input.nextLine();
                for (int i = 0; i < list.length; i++){
                     if (list[i] == original){
                     list[i]=newtitle;
                      }
                }            
               break;
              case 3: 
                System.out.println("You have chosen to print your collection");
                for (int i = 0; i < count+1;i++){
                    System.out.print(list[i]);
                    System.out.println();
                }
                break;
                case 4: 
                System.out.println("You have chosen to exit the application");
                System.out.println("Goodbye!");
                cont = false;
                break;        
        }
    }
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I changed the starting count to 0, because it felt more natural and moved the increment after the assignment.

I also implemented Java 17 switch, this way you can drop the break; keywords, and a text block.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String [] list = new String [100];
    int count = 0;
    boolean cont = true;
    while (cont){
        System.out.println("""
        Please make a selection:
        1 - Add a new movie
        2 - Update a movie
        3 - Print entire collection
        4 - Quit""");
        int response = input.nextInt();
        input.nextLine();
        switch (response) {
            case 1 -> {
                System.out.print("You have chosen to add a movie\n");
                System.out.print("Please enter a title:\n");
                String title = input.nextLine();
                list[count] = title;
                count++;
            }
            case 2 -> {
                System.out.println("You have chosen to update a movie");
                System.out.println("Please enter a title:");
                String original = input.nextLine();
                for (int i = 0; i < count; i++) {
                    if (list[i].equals(original)) {
                        System.out.println("Please enter a new title:");
                        String newTitle = input.nextLine();
                        list[i] = newTitle;
                        break;
                    }
                    System.out.println("Movie not found");
                }
            }
            case 3 -> {
                System.out.println("You have chosen to print your collection");
                for (int i = 0; i < count; i++) {
                    System.out.print(list[i]);
                    System.out.println();
                }
            }
            case 4 -> {
                System.out.println("You have chosen to exit the application");
                System.out.println("Goodbye!");
                cont = false;
            }
        }
    }
}

These lines didn't seem to serve any function:

String [] newlist = new String [count+1];    
newlist[count] = list[count];  

I also removed the dummy variable, you only need to call nextLine() to consume the \n before calling it again.

Apart from that, I would use a List instead of an array, that way you:

  • Can use size() instead of keeping a count
  • Don't have a fixed limit of movies
  • Can easily delete movies without open coded bulk copy
  • Don't need to reinvent indexOf()

Unless you are coding for some really old device with very limited memory, you are almost always better of with a List.

You might also want to add a default to the switch to handle other cases.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ use [a List] instead of an array[…]don't need to keep count […no fixed] limit […] easily delete movies without [open coded bulk] copy + no need to reinvent indexOf() \$\endgroup\$
    – greybeard
    Oct 31, 2021 at 12:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.