This program simply creates an array of movies with a star rating for each one. My problem is, I want to have code that adds the contents of the array if the movie is the same. So for my example I was using Frozen Ground as the movie. In my code it is rated 3 stars and 4 stars as well. How would I sum those numbers together without hardcoding.

I guess I am asking, how would I access the 3 of the first Frozen Ground rating and the 4 of the second, and then sum them together without using ratings[1] or ratings[3] as the access variables

public class MovieRating
{
	private String name;  // Name of the movie
	private int stars;    // A number from 1-5

	public MovieRating()
	{
		name = "Unknown";
		stars = 0;
	}

	public MovieRating(String movieName, int numStars)
	{
		name = movieName;
		stars = numStars;
	}

	public int getNumStars()
	{
		return stars;
	}

	public String getMovieName()
	{
		return name;
	}

	// Main method
	public static void main(String[] args)
	{
		// Create an array with 4 movie ratings
		MovieRating[] ratings = new MovieRating[4];


		// Some hypothetical ratings.  Note that we have to create a new MovieRating
		// object for each array entry.
		ratings[0] = new MovieRating("Big Miracle", 3);
		ratings[1] = new MovieRating("Frozen Ground", 3);
		ratings[2] = new MovieRating("Into The Wild", 5);
		ratings[3] = new MovieRating("Frozen Ground", 4);

		//COMPUTE COMMON SUM
             // RATING FOR "Frozen Ground" - 

 		 String s = "Frozen Ground";
 		 double average = 0;


 		 for (int i = 0; i < ratings.length; i++)
 		 {

				if (ratings[i].getMovieName().equals(s))
					{
					

					
					int sum = ratings[i].getNumStars();
					//System.out.println(sum);
					}


			//System.out.println(ratings[i].getNumStars());
		}




	}
}

Recommended Answers

All 2 Replies

Hmm, I can't think of any other way to access the elements(the rating Numbers) without using the array indexes.

I was thinking of having two extra variables.
The first variable will be to count the quantity of ratings for that certain movie.
The second variable will be a variable that holds all the accumulative value for the movie. (will act like the "cumulative sum")

Something like this

String s = "Frozen Ground";
 		double average = 0;
                int cSum = 0;
                int movieCount = 0;


 		 for (int i = 0; i < ratings.length; i++)
 		 {

				if (ratings[i].getMovieName().equals(s))
					{
					++movieCount;
                                                                                   

					
					int sum = ratings[i].getNumStars();
					cSum += sum;
					}


			//System.out.println(ratings[i].getNumStars());
		}

then just divide the cSum/movieCount and store it in average.

I see what you are doing Harana, thank you. My mind is so confined to one way, when there are so many.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.