Java Error Message. Unexpected Type, Required Variable, Found Value

Compilation error, unexpected type. required: variable found: value

Problem


The line
grades.get(i) += currDiff;
Will be processed as;
grades.get(i) = grades.get(i) + currDiff;

So basically the value of the sum will be assigned to the grades.get(i) which is not possible. ( As this is a value itself, not a variable)

Solution


What you probably need to change the code as;

grades.set(i, grades.get(i) + currDiff);

Of course I could be wrong about the solution depending on what you exactly want to do here.

Why am I getting the Error: unexpected type required: variable found: value

I believe you are trying to shuffle cards 1000 times randomly, soo the most simplistic approach IMHO would be something like this (assuming your cards are in ArrayList)

private void Shuffle() {
for (int p = 0; p < 1000; p++) {
int rand1 = (int) (Math.random() * 52);
int rand2 = (int) (Math.random() * 52);
Card card1 = cards.get(rand1);
Card card2 = cards.get(rand2);
cards.set(rand1, card2);
cards.set(rand2, card1);
}
}

Error: unexpected type required: variable found: value

In your code, stringSize is an integer, to store a string like "ok" or "not ok", you should use a String variable. Declare a String variable and store the value in that. For example -

int stringSize = (n_1 + n_2 + n_3);
String message = null;
if (stringSize < 20){
message = "ok";
}else if (stringSize > 20){
message = "not ok";
}

Moreover, String.valueOf() returns the string representation of the parameter where you cannot assign another value. For example, String.valueOf(stringSize) will return "15" if the value of stringSize is 15.


How do i get the stringSize to print either "ok" or "not ok"?

Just put the following line wherever you want to print the message. [following the example given above]

if(message != null){
System.out.println(message);
}


Related Topics



Leave a reply



Submit