0

I want to use a for loop to ask the user for a movie title, genre and rating and get for it to iterate three times and store in an array and display the information back to the user.

In the for loop in the code, I am not sure what to insert as "Movie 1" and how to get it to change to "Movie 2" and "Movie 3" after each iteration.

This is what I have so far :

import java.util.Scanner;
public class NewClass {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Creates a Movie array of size three.
Movie[] movies = new Movie[3];  
for (int i=0; i < movies.length; i++) {
// Initialises the 3 movie objects in the array.
movies[i] = new Movie();
}

// Loop will execute 3 times.
for (int x = 0; x < 3; x += 1){ 
    System.out.println("Please enter the title of Movie 1: ");
    System.out.println("Please enter the genre of Movie 1: ");
    System.out.println("Please enter the rating of Movie 1: ");
}

}
SMA
  • 36,381
  • 8
  • 49
  • 73
Chloe Conlon
  • 13
  • 1
  • 4
  • possible duplicate of [Java Scanner class reading strings](http://stackoverflow.com/questions/1466418/java-scanner-class-reading-strings) – Abhi Oct 26 '14 at 15:11

2 Answers2

0

Please post your Movie class to help us suggest a more detailed answer.

you can try something like this ...

Scanner sc=new Scanner(system.in);
for (int x = 0; x < 3; x++){ 
    System.out.println("Please enter the title of Movie 1: ");
    movies[i].title=sc.next();
    System.out.println("Please enter the genre of Movie 1: ");
    movies[i].genre=sc.next();
    System.out.println("Please enter the rating of Movie 1: ");
    movies[i].genre=sc.nextInt();
}

Use x++ instead of x+=1 , try using enhanced for loop in Java , and import java.util.* for the Scanner to work

Sainath S.R
  • 3,074
  • 9
  • 41
  • 72
0

You did most of the stuff right, just that you need to figure out what to do when as explained in comments below:

Scanner input = new Scanner(System.in);

// Creates a Movie array of size three.
//here you are declaring your array of movies of size 3
Movie[] movies = new Movie[3];
String name;
//lets ask user to enter movie name three times and simultaneously populate movie object and store them in an array.
for (int i=0; i < movies.length; i++) {
  System.out.println("Please enter the title of Movie " + (i+1));
  name = input.nextLine();
movies[i] = new Movie(name);
}

// Loop will execute 3 times and just print out the movie names that we populated.
for (int x = 0; x < movies.length; x += 1){ 
    System.out.println("movie " + (x+1) + " is " + movies[x].name);
}
SMA
  • 36,381
  • 8
  • 49
  • 73