Jump to content

What's up guys, I watched this video,https://mediaplayer.pearsoncmg.com/_cc_640x480/ph/streaming/esm/ecs_savitch_java_7/ProgrammingProject7-7.mp4, on how to add an array to the ratings portion of my code but I'm still getting errors when compiling. Could it be issues with the variable name ?

 

public class Movie
{
	private String name;
	private String rating;
	private int[] ratings;
	private int num = ;
	private double averageReview = ;

	public Movie()
	{
		name = "Blank Movie.";
		rating = "Not rated";
		ratings = new int[5];
		for (int i = ;i < 5; i++)
		ratings[i]=;
	}

	public Movie(String initialName)
	{
		name = initialName;
		rating = "Not rated";
	}

	public Movie(String initialName, String initialRating)
	{
		name = initialName;
		rating = initialRating;
	}

	public void setName(String newName)
	{
		name = newName;
	}
	public void setRating(String newRating)
	{
		rating = newRating;
	}

	public String getName()
	{
		return name;
	}
	public String getRating()
	{
		return rating;
	}
	public void addRating(int newReview)
	{
		if ((num < 1) || (num > 5))
			return;
		ratings[num-1]++;
		
	}
	public double getAverage()
	{
		averageReview = Math.round((ratings[]+1*ratings[1]+2*ratings[2]+3*ratings[3]+4*ratings[4]));
		return averageReview;
	}
	public String toString()
	{
		return "Name: " + name +
					 "\nRating: " + rating +
					 "\nAverage Review " + averageReview;
	}
	public void writeOutput()
	{
		System.out.println("Name: " + getName());  
		System.out.println("Rating: " + getRating());
		System.out.println("Average Review " + getAverage());
	}
	public boolean equal(Movie otherObject)
	{
		return (this.rating.equalsIgnoreCase(otherObject.rating)) && (this.averageReview == otherObject.averageReview);
	}

 public static void main(String[] args){
	 Movie movie = new Movie("Deadpool");
	 movie.setRating("R");
	 movie.addRating(5,5,5,5,4);
	 movie.writeOutput();
	 System.out.println();
	 Movie movietwo= new Movie("Zootopia");
	 movietwo.setRating("PG");
	 movietwo.addRating(5);
	 movietwo.addRating(5);
	 movietwo.addRating(5);
	 movietwo.addRating(4);
	 movietwo.addRating(4);
	 movietwo.writeOutput();
 }
 }

 

 

Link to comment
https://linustechtips.com/topic/583533-java-help-adding-an-array-to-a-int/
Share on other sites

Link to post
Share on other sites

public static void main(String[] args){
     Movie Movie = new Movie("Deadpool");
     Movie.setRating("R");
     Movie.addRating(5);
     Movie.addRating(5);
     Movie.addRating(5);
     Movie.addRating(4);
     Movie.addRating(5);
     Movie.writeOutput();
     System.out.println();
     Movie Movietwo= new Movie("Zootopia");
     Movietwo.setRating("PG");
     Movietwo.addRating(5);
     Movietwo.addRating(5);
     Movietwo.addRating(5);
     Movietwo.addRating(4);
     Movietwo.addRating(4);
     Movietwo.writeOutput();
 }

Make the variable "Movie" lowercase to differentiate from the class

i7 4790k | MSI Z97S SLI Krait Edition | G.Skill Ripjaws X 16 GB | Samsung 850 EVO 500 GB | 2x Seagate Barracuda 2TB | MSI GTX 970 Twin Frozr V | Fractal Design R4 | EVGA 650W

A gaming PC for your budget: $800 - $1000 - $1500 - $1800 - $2600 - $9001

Remember to quote people if you want them to see your reply!

Link to post
Share on other sites

 public Movie()
    {
        name = "Blank Movie.";
        rating = "Not rated";
        ratings = new int[5];
        for (int i = 0;i < 5; i++)
        (ratings=0;
    )
    }

 

To me, that looks like a compiler error straight up; You're trying to make the int[] with 5 elements equal to the integer 0 (do you want it to be null or have integers in it?). Also, turn those parentheses around the for-loop contents into curly braces, or get rid of them entirely. When dealing with the compiler errors, work from the top down, don't try to jump in randomly or work bottom-up; your later issues could be caused from your first compiler error. Other than that, use your compiler errors to tell you where the problem is (it'll give you the line and the character number where the error starts).

 

For readability I'd change "setRating" or "addRating" to something more explicit, same with "rating" and "ratings". Easy to confuse. That's a little picky though.

 

Without seeing the compiler errors, I hope that helps a bit.

 

Link to post
Share on other sites

2 minutes ago, Vylkire said:

 public Movie()
    {
        name = "Blank Movie.";
        rating = "Not rated";
        ratings = new int[5];
        for (int i = 0;i < 5; i++)
        (ratings=0;
    )
    }

 

To me, that looks like a compiler error straight up; You're trying to make the int[] with 5 elements equal to the integer 0 (do you want it to be null or have integers in it?). Also, turn those parentheses around the for-loop contents into curly braces, or get rid of them entirely. When dealing with the compiler errors, work from the top down, don't try to jump in randomly or work bottom-up; your later issues could be caused from your first compiler error. Other than that, use your compiler errors to tell you where the problem is (it'll give you the line and the character number where the error starts).

 

For readability I'd change "setRating" or "addRating" to something more explicit, same with "rating" and "ratings". Easy to confuse. That's a little picky though.

 

Without seeing the compiler errors, I hope that helps a bit.

 

3

Movie.java:18: error: ')' expected
        (ratings=0;
                     ^
Movie.java:18: error: not a statement
        (ratings=0;
        ^
Movie.java:19: error: illegal start of expression
    )
    ^
Movie.java:53: error: ')' expected
        if ((num < 1) || (num > 5)
                                  ^
Movie.java:58: error: class, interface, or enum expected
    public double getAverage()
           ^
Movie.java:61: error: class, interface, or enum expected
        return averageReview;
        ^
Movie.java:62: error: class, interface, or enum expected
    }
    ^
Movie.java:63: error: class, interface, or enum expected
    public String toString()
           ^
Movie.java:68: error: class, interface, or enum expected
    }
    ^
Movie.java:69: error: class, interface, or enum expected
    public void writeOutput()
           ^
Movie.java:72: error: class, interface, or enum expected
        System.out.println("Rating: " + getRating());
        ^
Movie.java:73: error: class, interface, or enum expected
        System.out.println("Average Review " + getAverage());
        ^
Movie.java:74: error: class, interface, or enum expected
        System.out.println(terrible + " people have rated this movie as terrible.");
        ^
Movie.java:75: error: class, interface, or enum expected
        System.out.println(bad + " people have rated this movie as bad.");
        ^
Movie.java:76: error: class, interface, or enum expected
        System.out.println(ok  + " people have rated this movie as OK.");
        ^
Movie.java:77: error: class, interface, or enum expected
        System.out.println(good + " people have rated this movie as good.");
        ^
Movie.java:78: error: class, interface, or enum expected
        System.out.println(great + " people have rated this movie as great.");
        ^
Movie.java:79: error: class, interface, or enum expected
    }
    ^
Movie.java:80: error: class, interface, or enum expected
    public boolean equal(Movie otherObject)
           ^
Movie.java:83: error: class, interface, or enum expected
    }
    ^
Movie.java:85: error: class, interface, or enum expected
 public static void main(String[] args){
               ^
Movie.java:87: error: class, interface, or enum expected
     movie.setRating("R");
     ^
Movie.java:88: error: class, interface, or enum expected
     movie.addRating(5);
     ^
Movie.java:89: error: class, interface, or enum expected
     movie.addRating(5);
     ^
Movie.java:90: error: class, interface, or enum expected
     movie.addRating(5);
     ^
Movie.java:91: error: class, interface, or enum expected
     movie.addRating(4);
     ^
Movie.java:92: error: class, interface, or enum expected
     movie.addRating(5);
     ^
Movie.java:93: error: class, interface, or enum expected
     movie.writeOutput();
     ^
Movie.java:94: error: class, interface, or enum expected
     System.out.println();
     ^
Movie.java:95: error: class, interface, or enum expected
     Movie movietwo= new movie("Zootopia");
     ^
Movie.java:96: error: class, interface, or enum expected
     movietwo.setRating("PG");
     ^
Movie.java:97: error: class, interface, or enum expected
     movietwo.addRating(5);
     ^
Movie.java:98: error: class, interface, or enum expected
     movietwo.addRating(5);
     ^
Movie.java:99: error: class, interface, or enum expected
     movietwo.addRating(5);
     ^
Movie.java:100: error: class, interface, or enum expected
     movietwo.addRating(4);
     ^
Movie.java:101: error: class, interface, or enum expected
     movietwo.addRating(4);
     ^
Movie.java:102: error: class, interface, or enum expected
     movietwo.writeOutput();
     ^
Movie.java:103: error: class, interface, or enum expected
 }
 ^
38 errors

 

Link to post
Share on other sites

8 minutes ago, Sebastian Kurpiel said:

Movie.java:18: error: ')' expected
        (ratings=0;
                     ^
Movie.java:18: error: not a statement
        (ratings=0;
        ^
Movie.java:19: error: illegal start of expression
    )
    ^
Movie.java:53: error: ')' expected
        if ((num < 1) || (num > 5)
                                  ^

 

So yeah, those parentheses should be curly braces or not there in that first one. You can see it doesn't like how you've tried to do (ratings=0;).

You're trying to set ratings(the array) = 0;(the integer), which won't work. You want something like

   for(blah){ratings['i']=0;}  //remove ' ' because it thinks it should be an italics tag in html

I'm not sure why you'd want to fill it with 0, but hey, it's your array.

 

As to if statement, check your parentheses.

I see if TWO OPEN - ONE CLOSE - ONE OPEN - ONE CLOSE...(I should see two closes here).

 

And as HPWebCamable has said, change your "Movie Movie" to "Movie movie", then call it with "movie.stuff"

 

Try that first, and see what you get.

Link to post
Share on other sites

12 minutes ago, Vylkire said:

So yeah, those parentheses should be curly braces or not there in that first one. You can see it doesn't like how you've tried to do (ratings=0;).

You're trying to set ratings(the array) = 0;(the integer), which won't work. You want something like

   for(blah){ratings['i']=0;}  //remove ' ' because it thinks it should be an italics tag in html

I'm not sure why you'd want to fill it with 0, but hey, it's your array.

 

As to if statement, check your parentheses.

I see if TWO OPEN - ONE CLOSE - ONE OPEN - ONE CLOSE...(I should see two closes here).

 

And as HPWebCamable has said, change your "Movie Movie" to "Movie movie", then call it with "movie.stuff"

 

Try that first, and see what you get.

 
 

Movie.java:57: error: class, interface, or enum expected
    public double getAverage()
           ^
Movie.java:60: error: class, interface, or enum expected
        return averageReview;
        ^
Movie.java:61: error: class, interface, or enum expected
    }

Receiving more errors but I think it would work better if I do the a couple at a time

.

System.out.println(terrible + " people have rated this movie as terrible.");
		System.out.println(bad + " people have rated this movie as bad.");
		System.out.println(ok  + " people have rated this movie as OK.");
		System.out.println(good + " people have rated this movie as good.");
		System.out.println(great + " people have rated this movie as great.");

I removed this and it got rid of some errors, it wasn't needed anyway.

 

Link to post
Share on other sites

Yeah, definitely do a couple at a time.

 

You're going to have to go through and check your curly brackes, e.g "addRating()" has  } but no {, so you're closing your class before having access to your functions later. Why? Also check what you're passing/working with in each function too. "addRating()", for example. How does it get access to "ratings[]"? What is "newReview" doing in that function? Where did num come from, what's it for?

 

Link to post
Share on other sites

12 hours ago, Vylkire said:

Yeah, definitely do a couple at a time.

 

You're going to have to go through and check your curly brackes, e.g "addRating()" has  } but no {, so you're closing your class before having access to your functions later. Why? Also check what you're passing/working with in each function too. "addRating()", for example. How does it get access to "ratings[]"? What is "newReview" doing in that function? Where did num come from, what's it for?

 

 
 
 
 
private int[] ratings;
private int num = ;

I fixed most of the code and this is what I'm receiving when I run it.

Name: Deadpool
Rating: R
Exception in thread "main" java.lang.NullPointerException
    at Movie.getAverage(Movie.java:61)
    at Movie.writeOutput(Movie.java:74)
    at Movie.main(Movie.java:89)

I think it has something to do with the ratings not being in an array...

Question: how would I add arrays to the main part of the code?

Link to post
Share on other sites

Not entirely sure what you're looking for. Right away I notice the first for loop you have is completely missing braces. There's several other areas where things are missing. Are you using an IDE? Something with syntax highlighting or a debugger would really help you out.

 

As an example, your "addRating" has a parameter of a single integer, but you're attempting to pass in five.

 

You might be helped by sitting down and diagramming what you want the code to do.

 

If you're not assigning a value when you're declaring variables why is there an assignment operator?

Link to post
Share on other sites

On 18/04/2016 at 2:11 AM, Sebastian Kurpiel said:

private int[] ratings;
private int num = ;

I fixed most of the code and this is what I'm receiving when I run it.

Name: Deadpool
Rating: R
Exception in thread "main" java.lang.NullPointerException
    at Movie.getAverage(Movie.java:61)
    at Movie.writeOutput(Movie.java:74)
    at Movie.main(Movie.java:89)

I think it has something to do with the ratings not being in an array...

Question: how would I add arrays to the main part of the code?

 

public double getAverage()
	{
		averageReview = Math.round((ratings[]+1*ratings[1]+2*ratings[2]+3*ratings[3]+4*ratings[4]));
		return averageReview;
	}

 

So that's the method that seems to be throwing the exception, I'm not sure what you're trying to do with that bit of math, but ratings[]+1 is trying to "add (1 times the ratings[] element in the 2nd position) to the ratings array[]" and I'd say that isn't a very popular thought for the compiler. It's probably what's telling you about a null pointer. If you're just trying to get the average of the values you need to add the elements together, then divide them by your number of elements. Unless you want some alternate scaling that gets you a value greater than 5 (even then, I'm not sure what you're up to there).

 

As jslowik has said, you're trying to pass in multiple values here;

public void addRating(int newReview)
	{
		if ((num < 1) || (num > 5))
			return;
		ratings[num-1]++;
		
	}

 

That's going to fail. You're telling Java to expect a single int and you're passing in five. This won't work.

You're then saying that there's a value named "num" that java will evaluate for being 1 < num < 5

If it is, you're saying "go to the ratings array and find the "num-th minus 1" element and increment it by 1"

Again, as jslowik has said; Num never gets assigned to anything. As above, if you're not assigning, don't use the assignment operator.

 

So let's assume you want "num to be "newReview" and walk through it verbally;

 

addRating, I want you to take a single integer, that I'm naming newReview. Check if newReview is less than one OR greater than five, if it is, don't do anything. If it's between 1 and 5, find the element in the array ratings at the newReview-th minus 1 position and increment it by 1.

 

So a couple of questions; assuming that you're assigning "ratings[]" as a global variable - what are you actually doing with it? Do you want to add the rating value to the array or are you actually trying to increment the value at a position? From what I can see, you've never assigned anything to your array.

If you just want the array filled with set values, don't iterate through it. You've only got five values, you can just assign the values to it statically.

 

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

 

If in doubt, check the documentation. It's actually pretty handy.

 

Hopefully this doesn't sound as harsh as it does in my head, but it feels like you don't really know what things are doing in your code (I've been there, a lot).

Take a step back and draw yourself diagrams, or write it out as text(or pseudocode)

"move.whatever will do this. this will do that."

 

It helps more than you would expect.

 

If you're not understanding why things aren't behaving as you expect, go back to basics and find out what each of your components should be doing.

 

P.S. Sorry about the wall of text.

Link to post
Share on other sites

public class Movie {
    private String name;
    private String rating;
    private int[] ratings;
    /*
     * Why are these not init'd to a value?
     */
    private int num = ;
    private double averageReview = ;

    public Movie() {
        name = "Blank Movie.";
        rating = "Not rated";
        ratings = new int[5];
       /*
		* Put {} around the for loop and all if statements, it will make debugging much easier. 
        * Also, if filling an array with the same values, use Arrays.fill
        * https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#fill(int[],%20int)
		*/
        for (int i = ; i < 5; i++) // What is i equal to? You should be assigning it a value here.
            ratings[i] =; // What are you trying to set this equal to? Assign it a value here.
    }

    public Movie(String initialName) {
        name = initialName;
        rating = "Not rated";
    }

    public Movie(String initialName, String initialRating) {
        name = initialName;
        rating = initialRating;
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getRating() {
        return rating;
    }

    public void setRating(String newRating) {
        rating = newRating;
    }

    public void addRating(int newReview) {
        // Put {} around all if statements, it will make debugging much easier.
        if ((num < 1) || (num > 5))
            return;
        ratings[num - 1]++; // What is the point of this line? You will lose newRating as you do not save it anywhere. 
        // Ratings is also never init'd to be an array of 5 int unless they use the blank constructor.
    }

    public double getAverage() {
        // There is no element at the [] position. Put a real value in here (0, in this case). 
        // Also, where aren't you just summing the review and dividing by the number of reviews? 
        // I'm not really sure what kind of average you're trying to get here.
        averageReview = Math.round((ratings[]+1 * ratings[1] + 2 * ratings[2] + 3 * ratings[3] + 4 * ratings[4]));
        return averageReview;
    }

    public String toString() {
        return "Name: " + name +
                "\nRating: " + rating +
                "\nAverage Review " + averageReview;
    }

    public void writeOutput() {
        System.out.println("Name: " + getName());
        System.out.println("Rating: " + getRating());
        System.out.println("Average Review " + getAverage());
    }

    /*
     * Shouldn't this also check if the names are the same?
     * Also, you are trying to access private variables... You must use their getters if they are private.
     */
    public boolean equal(Movie otherObject) {
        return (this.rating.equalsIgnoreCase(otherObject.rating)) && (this.averageReview == otherObject.averageReview);
    }

    public static void main(String[] args) {
        Movie movie = new Movie("Deadpool");
        movie.setRating("R");
        movie.addRating(5, 5, 5, 5, 4); // addRating expects 1 number, so unless you change it to use a variable length argument, this will not work
        movie.writeOutput();
        System.out.println();
        Movie movietwo = new Movie("Zootopia"); // Java uses camelCase notation, so this should be movieTwo.
        movietwo.setRating("PG");
        movietwo.addRating(5);
        movietwo.addRating(5);
        movietwo.addRating(5);
        movietwo.addRating(4);
        movietwo.addRating(4);
        movietwo.writeOutput();
    }
}

I've commented your code to highlight some issued. Let me know if you want me to explain further. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

@Blade of Grass @Vylkire somewhere in between pasting my code the "0"s disappeared.

Could you better explain why this line of code won't work?

	public double getAverage()
	{
		averageReview = Math.round((ratings[]+1*ratings[1]+2*ratings[2]+3*ratings[3]+4*ratings[4])/5);
		return averageReview;

I wrote this code a month ago for a homework assignment and in my new homework assignment the professor wanted us to add an array so I followed this video that my textbook provided to add the array part... The point of the program is to display the name, rating(pg,r,g,etc), and the average rating it received like a 5 or 4.6.

 

This is my only error atm.

Movie.java:81: error: cannot find symbol
     movie.addRating[5]=5;
          ^
  symbol:   variable addRating
  location: variable movie of type Movie
1 error

I'm trying to figure out how to add the rating in array form.

Capture.PNG

Link to post
Share on other sites

2 hours ago, Sebastian Kurpiel said:

@Blade of Grass @Vylkire somewhere in between pasting my code the "0"s disappeared.

Could you better explain why this line of code won't work?


	public double getAverage()
	{
		averageReview = Math.round((ratings[]+1*ratings[1]+2*ratings[2]+3*ratings[3]+4*ratings[4])/5);
		return averageReview;

I wrote this code a month ago for a homework assignment and in my new homework assignment the professor wanted us to add an array so I followed this video that my textbook provided to add the array part... The point of the program is to display the name, rating(pg,r,g,etc), and the average rating it received like a 5 or 4.6.

 

This is my only error atm.

Movie.java:81: error: cannot find symbol
     movie.addRating[5]=5;
          ^
  symbol:   variable addRating
  location: variable movie of type Movie
1 error

I'm trying to figure out how to add the rating in array form.

Capture.PNG

Average (arithmetic mean) is simply the sum of all elements over the number of elements. 

img5D.gif

 

But instead in your code you are multiplying the ratings by which position they are at.

ratings[] = 2;
ratings[1] = 2;
ratings[2] = 2;
ratings[3] = 2;
ratings[4] = 2;

double yourAverage = (ratings[]+1*ratings[1]+2*ratings[2]+3*ratings[3]+4*ratings[4])/5;
// This will be equal to 4.4
// The average should actually be 2

double realAverage = IntStream.of(ratings).average();
// This will be equal to 2

 

Now, specifically with your error message:

movie.addRating[5]=5;

Movie#addRating is an instance method, so you can't assign it a value in this way (using an equals sign or [] especially). If you want to be able to modify specific rating values then you should overload the addRating method and make one that takes two parameters, index and newValue, which then overwrites the rating at the index with the new value.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

10 minutes ago, Blade of Grass said:

The issue with that line of code is with the first addition. You wrote 


(ratings[]+1*ratings[1]+2*ratings[2]+3*ratings[3]+4*ratings[4])/5

The issue here is with the first array access


ratings[]

You're trying to access an element at a position, but you don't actually tell it which position you want. This is not allowed

 

Also, the average (arithmetic mean) is simply the sum of all elements over the number of elements. 

img5D.gif

 

But instead in your code you are multiplying the ratings by which position they are at.


ratings[] = 2;
ratings[1] = 2;
ratings[2] = 2;
ratings[3] = 2;
ratings[4] = 2;

double yourAverage = (ratings[]+1*ratings[1]+2*ratings[2]+3*ratings[3]+4*ratings[4])/5;
// This will be equal to 4.4
// The average should actually be 2

double realAverage = IntStream.of(ratings).average();
// This will be equal to 2

 

Now, specifically with your error message:


movie.addRating[5]=5;

Movie#addRating is an instance method, so you can't assign it a value in this way (using an equals sign especially). If you want to be able to modify specific rating values then you should overload the addRating method and make one that takes two parameters, index and newValue, which then overwrites the rating at the index with the new value.

 

It's giving me a cannot find symbol IntStream error.

How would I fill the Array in the main part of my code? Would that be the part where you mentioned to overload the addRating?

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

It's giving me a cannot find symbol IntStream error.

How would I fill the Array in the main part of my code? Would that be the part where you mentioned to overload the addRating?

Did you import IntStream? Are you using Java 8?

 

For the blank constructor I would suggest using Array.fill

For the non blank constructor I would also suggest using Array.fill ;) The array should always be initialized to a value so that you don't have to worry about null point exceptions. 

 

 

Now, for the ratings, is the array just going to be populated once or is it possible that you need to change the elements later on? If it's the latter then I would suggest creating a setter alongside the adder and getter to make your life easier. For example:

boolean[] test = new boolean[5];
int POS = ;

void addTest (boolean val) {
	if (POS == test.length()) {
		// One way to handle people trying to add too many values.
		throw new ArrayAlreadyFilledException;
	}
	test[POS++] = val;
}

void setTest(int index, boolean val) {
	test[index] = val;
}

boolean getTest(int index) {
	return test[index];
}

This would allow you assign values in the array, and then change them later on (if you need it) by calling the #setTest method.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

21 minutes ago, Blade of Grass said:

Did you import IntStream? Are you using Java 8?

 

For the blank constructor I would suggest using Array.fill

For the non blank constructor I would also suggest using Array.fill ;) The array should always be initialized to a value so that you don't have to worry about null point exceptions. 

 

 

Now, for the ratings, is the array just going to be populated once or is it possible that you need to change the elements later on? If it's the latter then I would suggest creating a setter alongside the adder and getter to make your life easier. For example:


boolean[] test = new boolean[5];
int POS = ;

void addTest (boolean val) {
	if (POS == test.length()) {
		// One way to handle people trying to add too many values.
		throw new ArrayAlreadyFilledException;
	}
	test[POS++] = val;
}

void setTest(int index, boolean val) {
	test[index] = val;
}

boolean getTest(int index) {
	return test[index];
}

This would allow you assign values in the array, and then change them later on (if you need it) by calling the #setTest method.

 

Movie.java:59: error: incompatible types: OptionalDouble cannot be converted to double
        averageReview = IntStream.of(ratings).average();

For the array.fill, could I simplely write movie.arrayfill(ratings, 5 5 5 5 5);

Link to post
Share on other sites

4 minutes ago, Sebastian Kurpiel said:

Movie.java:59: error: incompatible types: OptionalDouble cannot be converted to double
        averageReview = IntStream.of(ratings).average();

For the array.fill, could I simplely write movie.arrayfill(ratings, 5 5 5 5 5);

My mistake, change averageReview to be of OptionalDouble, but if you want to get the value from it as a double you need to do averageReview.getAsDouble() to "convert" it.

 

Array.fill is actually a static method, so it should be invoked as follows:

Array.fill(ratings, 5);

This will fill the entire ratings array with the value 5. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

6 minutes ago, Blade of Grass said:

My mistake, change averageReview to be of OptionalDouble, but if you want to get the value from it as a double you need to do averageReview.getAsDouble() to "convert" it.

 

Array.fill is actually a static method, so it should be invoked as follows:


Array.fill(ratings, 5);

This will fill the entire ratings array with the value 5. 

 

Movie.java:58: error: cannot find symbol
    public OpitionalDouble getAverage()
           ^
  symbol:   class OpitionalDouble
  location: class Movie
Movie.java:60: error: incompatible types: OptionalDouble cannot be converted to double
        averageReview = IntStream.of(ratings).average();
                                                     ^
Movie.java:83: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,5);
                ^
Movie.java:83: error: cannot find symbol
     Array.fill(ratings,5);
     ^
  symbol:   variable Array
  location: class Movie
Movie.java:88: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,4);
                ^
Movie.java:88: error: cannot find symbol
     Array.fill(ratings,4);
     ^
  symbol:   variable Array
  location: class Movie
6 errors

How would one go about changing it to optional double? and should I change the variable name? 

Link to post
Share on other sites

1 minute ago, Sebastian Kurpiel said:

Movie.java:58: error: cannot find symbol
    public OpitionalDouble getAverage()
           ^
  symbol:   class OpitionalDouble
  location: class Movie
Movie.java:60: error: incompatible types: OptionalDouble cannot be converted to double
        averageReview = IntStream.of(ratings).average();
                                                     ^
Movie.java:83: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,5);
                ^
Movie.java:83: error: cannot find symbol
     Array.fill(ratings,5);
     ^
  symbol:   variable Array
  location: class Movie
Movie.java:88: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,4);
                ^
Movie.java:88: error: cannot find symbol
     Array.fill(ratings,4);
     ^
  symbol:   variable Array
  location: class Movie
6 errors

How would one go about changing it to optional double? and should I change the variable name? 

Can you show me your full code? I think I know what's going on, but I'd like to explain specifically what's wrong. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

2 minutes ago, Blade of Grass said:

Can you show me your full code? I think I know what's going on, but I'd like to explain specifically what's wrong. 

 
 

Is it the ratings being declared private?

import java.util.stream.IntStream;
import java.util.Arrays;

public class Movie
{
	private String name;
	private String rating;
	private int[] ratings;
	private int num = ;
	private double averageReview;

	public Movie()
	{
		name = "Blank Movie.";
		rating = "Not rated";
		ratings = new int[5];
		{for (int i = ;i < 5; i++)
		ratings[i]=;
		}
	}

	public Movie(String initialName)
	{
		name = initialName;
		rating = "Not rated";
	}

	public Movie(String initialName, String initialRating)
	{
		name = initialName;
		rating = initialRating;
	}

	public void setName(String newName)
	{
		name = newName;
	}
	public void setRating(String newRating)
	{
		rating = newRating;
	}

	public String getName()
	{
		return name;
	}
	public String getRating()
	{
		return rating;
	}
	public void addRating(int[] ratings)
	{
		if ((num < 1) || (num > 5))
			return;
		ratings[num-1]++;
		
	}
	public OpitionalDouble getAverage()
	{
		averageReview = IntStream.of(ratings).average();
		return averageReview;
	}
	public String toString()
	{
		return "Name: " + name +
					 "\nRating: " + rating +
					 "\nAverage Review " + averageReview;
	}
	public void writeOutput()
	{
		System.out.println("Name: " + getName());  
		System.out.println("Rating: " + getRating());
		System.out.println("Average Review " + getAverage());
	}
	public boolean equal(Movie otherObject)
	{
		return (this.rating.equalsIgnoreCase(otherObject.rating)) && (this.averageReview == otherObject.averageReview);
	}

 public static void main(String[] args){
	 Movie movie = new Movie("Deadpool");
	 movie.setRating("R");
	 Array.fill(ratings,5);
	 movie.writeOutput();
	 System.out.println();
	 Movie movieTwo= new Movie("Zootopia");
	 movieTwo.setRating("PG");
	 Array.fill(ratings,4);
	 movieTwo.writeOutput();
 }
 }

 

Link to post
Share on other sites

3 minutes ago, Sebastian Kurpiel said:

Is it the ratings being declared private?


import java.util.stream.IntStream;
import java.util.Arrays;

public class Movie
{
	private String name;
	private String rating;
	private int[] ratings;
	private int num = ;
	private double averageReview; // This needs to be OptionalDouble.

	public Movie()
	{
		name = "Blank Movie.";
		rating = "Not rated";
		ratings = new int[5];
		{for (int i = ;i < 5; i++)
		ratings[i]=;
		}
	}

	public Movie(String initialName)
	{
		name = initialName;
		rating = "Not rated";
	}

	public Movie(String initialName, String initialRating)
	{
		name = initialName;
		rating = initialRating;
	}

	public void setName(String newName)
	{
		name = newName;
	}
	public void setRating(String newRating)
	{
		rating = newRating;
	}

	public String getName()
	{
		return name;
	}
	public String getRating()
	{
		return rating;
	}
	// Still need to fix this
	public void addRating(int[] ratings)
	{
		if ((num < 1) || (num > 5))
			return;
		ratings[num-1]++;
		
	}
	public OpitionalDouble getAverage() // This isn't what needs to be OptionalDouble, this should remain as double.
	{
		averageReview = IntStream.of(ratings).average();
		return averageReview; // Remember that you need to call a getter on this now. 
	}
	public String toString()
	{
		return "Name: " + name +
					 "\nRating: " + rating +
					 "\nAverage Review " + averageReview;
	}
	public void writeOutput()
	{
		System.out.println("Name: " + getName());  
		System.out.println("Rating: " + getRating());
		System.out.println("Average Review " + getAverage());
	}
	public boolean equal(Movie otherObject)
	{
        // These variables are private and you will be unable to access them. Use their getters.
		return (this.rating.equalsIgnoreCase(otherObject.rating)) && (this.averageReview == otherObject.averageReview);
	}

 public static void main(String[] args){
	 Movie movie = new Movie("Deadpool");
	 movie.setRating("R");
	 Array.fill(ratings,5); // This is in the wrong place, this should be in the constructor for the class.
	 movie.writeOutput();
	 System.out.println();
	 Movie movieTwo= new Movie("Zootopia");
	 movieTwo.setRating("PG"); 
	 Array.fill(ratings,4); // This is in the wrong place, this should be in the constructor for the class.
	 movieTwo.writeOutput();
 }
 }

 

I've annotated your code again. 

 

Do movies always have 5 ratings? 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

11 minutes ago, Blade of Grass said:

I've annotated your code again. 

 

Do movies always have 5 ratings? 

 
 

Movie.java:10: error: cannot find symbol
    private OpitionalDouble averageReview;
            ^
  symbol:   class OpitionalDouble
  location: class Movie
Movie.java:83: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,5);
                ^
Movie.java:83: error: cannot find symbol
     Array.fill(ratings,5);
     ^
  symbol:   variable Array
  location: class Movie
Movie.java:88: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,4);
                ^
Movie.java:88: error: cannot find symbol
     Array.fill(ratings,4);
     ^
  symbol:   variable Array
  location: class Movie
5 errors

I was using 5 in my original code for my last homework assignment 

original code

public class Movie
{
	private String name;
	private String rating;
	private double averageReview = ;
	private int terrible = ;
	private int bad = ;
	private int ok = ;
	private int good = ;
	private int great = ;
	
	public Movie()
	{
		name = "Blank Movie.";
		rating = "Not rated";
	}

	public Movie(String initialName)
	{
		name = initialName;
		rating = "Not rated";
	}

	public Movie(String initialName, String initialRating)
	{
		name = initialName;
		rating = initialRating;
	}

	public void setName(String newName)
	{
		name = newName;
	}
	public void setRating(String newRating)
	{
		rating = newRating;
	}

	public String getName()
	{
		return name;
	}
	public String getRating()
	{
		return rating;
	}
	public void addRating(int newReview)
	{
		switch (newReview)
		{
			case 1: 
				terrible ++;
				break;
			case 2: 
				bad ++;
				break;
			case 3: 
				ok ++;
				break;
			case 4: 
				good ++;
				break;
			case 5: 
				great ++;
				break;
			default: 
				System.out.println("A value other then 1,2,3,4, or 5 was entered for Review.");
				break;
		}
	}
	public double getAverage()
	{
		averageReview = Math.round(((terrible * 1) + (bad * 2) + (ok * 3) + (good * 4) + (great * 5))/5.00);
		return averageReview;
	}
	public String toString()
	{
		return "Name: " + name +
					 "\nRating: " + rating +
					 "\nAverage Review " + averageReview;
	}
	public void writeOutput()
	{
		System.out.println("Name: " + getName());  
		System.out.println("Rating: " + getRating());
		System.out.println("Average Review " + getAverage());
		System.out.println(terrible + " people have rated this movie as terrible.");
		System.out.println(bad + " people have rated this movie as bad.");
		System.out.println(ok  + " people have rated this movie as OK.");
		System.out.println(good + " people have rated this movie as good.");
		System.out.println(great + " people have rated this movie as great.");
	}
	public boolean equal(Movie otherObject)
	{
		return (this.rating.equalsIgnoreCase(otherObject.rating)) && (this.averageReview == otherObject.averageReview);
	}

 public static void main(String[] args){
	 Movie Movie = new Movie("Deadpool");
	 Movie.setRating("R");
	 Movie.addRating(5);
	 Movie.addRating(5);
	 Movie.addRating(5);
	 Movie.addRating(4);
	 Movie.addRating(5);
	 Movie.writeOutput();
	 System.out.println();
	 Movie Movietwo= new Movie("Zootopia");
	 Movietwo.setRating("PG");
	 Movietwo.addRating(5);
	 Movietwo.addRating(5);
	 Movietwo.addRating(5);
	 Movietwo.addRating(4);
	 Movietwo.addRating(4);
	 Movietwo.writeOutput();
 }
 }

I feel sorta lost with the one I'm now editing, would it be possible to create another movie class that just reads the array part and insert in here?

 

Link to post
Share on other sites

2 minutes ago, Sebastian Kurpiel said:

Movie.java:10: error: cannot find symbol
    private OpitionalDouble averageReview;
            ^
  symbol:   class OpitionalDouble
  location: class Movie

Did you import OptionalDouble?

2 minutes ago, Sebastian Kurpiel said:


Movie.java:83: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,5);
                ^

Movie.java:83: error: cannot find symbol
     Array.fill(ratings,5);
     ^
  symbol:   variable Array
  location: class Movie

Movie.java:88: error: non-static variable ratings cannot be referenced from a static context
     Array.fill(ratings,4);
                ^
Movie.java:88: error: cannot find symbol
     Array.fill(ratings,4);
     ^
  symbol:   variable Array
  location: class Movie

This MUST be done in the object constructor, this CANNOT be done in the "main" method. This code replaces

for (int i = ;i < 5; i++) {
	ratings[i]=;
}

Now, if you want to fill the ratings array afterwards with all of the same value you could make a method that does that, it could look something like:

void fillRatings(int ratingsVal) {
	Array.fill(ratings, ratingsVal);
}

 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

On 4/21/2016 at 8:01 PM, Blade of Grass said:

Did you import OptionalDouble?

This MUST be done in the object constructor, this CANNOT be done in the "main" method. This code replaces


for (int i = ;i < 5; i++) {
	ratings[i]=;
}

Now, if you want to fill the ratings array afterwards with all of the same value you could make a method that does that, it could look something like:


void fillRatings(int ratingsVal) {
	Array.fill(ratings, ratingsVal);
}

 

 
 

I ended up redoing everything in my code and it worked somehow... lol Thanks for all the help guys

Final Code:

public class Movie
{
	private String name;
	private String MPAArating;
	private double averageReview = ;
	private int[] ratings;
	
	public Movie()
	{
		name = "Blank Movie.";
		MPAArating = "Not rated";
		ratings = new int[5];
		for (int i = ; i<5; i++)
	{
		ratings[i]=;
	}
	}
	public Movie(String initialName)
	{
		name = initialName;
		MPAArating = "Not rated";
      	ratings = new int[5];
		for (int i = ; i<5; i++)
	{
		ratings[i]=;
	}
	}
	
	public Movie(String initialName, String initialRating)
	{
		name = initialName;
		MPAArating = initialRating;
      	ratings = new int[5];
		for (int i = ; i<5; i++)
	{
		ratings[i]=;
	}
	}

	public void setName(String newName)
	{
		name = newName;
	}
	public void setRating(String newRating)
	{
		MPAArating = newRating;
	}

	public String getName()
	{
		return name;
	}
	public String getRating()
	{
		return MPAArating;
	}
	public void addRating(int num)
	{
      if((num < 1) || (num > 5))
		return;
		ratings[num-1]++;
	}
	public double getAverage(){
	{
		int total = ;
		for(int i = ; i< 5; i++)
        {
			total += (i+1)*ratings[i];
        }
		return total/5.0;
	}
    }
	public String toString()
	{
		return "Name: " + name +
					 "\nRating: " + MPAArating +
					 "\nAverage Review " + averageReview;
	}
	public void writeOutput()
	{
		System.out.println("Name: " + getName());  
		System.out.println("Rating: " + getRating());
		System.out.println("Average Review " + getAverage());
	}

 public static void main(String[] args){
	 Movie Movie = new Movie("DeadPool");
	 Movie.setRating("R");
     Movie.addRating(5);
	 Movie.addRating(5);
	 Movie.addRating(5);
	 Movie.addRating(5);
	 Movie.addRating(5);
	 Movie.writeOutput();
	 System.out.println();
	 Movie Movietwo= new Movie("Zootopia");
	 Movietwo.setRating("PG");
	 Movietwo.addRating(5);
	 Movietwo.addRating(5);
	 Movietwo.addRating(5);
	 Movietwo.addRating(4);
	 Movietwo.addRating(4);
	 Movietwo.writeOutput();
 }
 }

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×