-4

I often have a problem picking up what movie I should watch. So, I decided to make a simple program to randomize my pick but got stuck on the first step. I want to know how to assign Strings - that are the movie names- to an array. Here is my unfinished code

    Scanner input = new Scanner(System.in);

  System.out.println("How many movies are you considering?");
  int number = input.nextInt();
  String[] movies = new String[number];

  System.out.println("Enter movie titles...");

  for(int i=1; i <= number; i++){
     System.out.print(i + "- ");
     movies[i] = input.next();
     System.out.println();
  }
zawawiza
  • 1
  • 1

1 Answers1

0

Your code is already doing that. However, you are getting an ArrayIndexOutOfBoundsException because your loop is set up to insert values into the array up to an index which does not exist.

Array indices are zero-based. This means that, if you have an array of size 5, its indices are numbered 0, 1, 2, 3, and 4. You are currently treating the array as if it was one-based; that is, as if an array of size 5 had indices 1-5 rather than 0-4.

To fix this, i should initially be equal to 0, and your loop should terminate when i < number instead of i <= number.

Bethany Louise
  • 646
  • 7
  • 13
  • I tried doing that before but when asked the user to plug in the names it started with zero. I want it to start with "1- " not "0- ". – zawawiza Dec 23 '16 at 20:15
  • Have it print i+1 instead of i. – Bethany Louise Dec 23 '16 at 20:24
  • I fixed that problem. But now I can't figure out why when I plug in values such as "Fight Club" it plugs in fight in array #0 and club in array #1. – zawawiza Dec 23 '16 at 20:41
  • You should be using `input.nextLine()` instead of `input.next()` to get the entire line rather than a single word. Sorry, I didn't catch that when I tested out your code. – Bethany Louise Dec 23 '16 at 20:43
  • idk why it skips the first element of the array and puts it as empty. – zawawiza Dec 23 '16 at 20:49
  • Okay, sorry, that was my own mistake. I referred back to the Scanner documentation, and I think `input.nextLine()` is not working as I assumed due to the fact that you are not beginning a new line for each input. I did some searching for a solution and found that `input.next(".+")` works; this just tells it to grab all text entered (but still terminates upon hitting Enter) without needing it to be on a new line. – Bethany Louise Dec 23 '16 at 21:08
  • can you please check my code, I tried many different things including changing the scanner but it still messes up when I plug in values for the array – zawawiza Dec 24 '16 at 14:36
  • `Scanner input = new Scanner(System.in); System.out.println("How many movies are you considering?"); int number = input.nextInt(); String[] movies = new String[number]; System.out.println("Enter movie titles..."); for(int i=0; i < number; i++){ System.out.print("- "); movies[i] = input.next(".+"); System.out.println(); } // End of for-loop Random rand = new Random(); int randMovie = rand.nextInt(number); System.out.println("WATCH: " + movies[randMovie]);` – zawawiza Dec 24 '16 at 14:37