El3ctr0n1c4
El3ctr0n1c4

Reputation: 400

unexpected type error

I'm new at Java, I study on strings and i want a string to be reversed. Here is my code

String myStr = "abcdef"; String reversed = "";
for(int j=myStr.length()-1;j>=0;j--) {
    myStr.charAt(j) += reversed;
}

But it gives me an error message:

****.java:14: error: unexpected type
required: variable
found:    value

But when I print it by System.out.print(reversed), it prints reversed correctly. What is the difference between variable and value? Why it can give me correct answer in spite of giving me an error message? I'll appreciate your answers, thanks

Upvotes: 5

Views: 62828

Answers (7)

KANJICODER
KANJICODER

Reputation: 3885

"unexpected type" error can happen if you screw up a "Yoda Expression":


Example:


if(null= my_variable){}; //:WRONG#1 (malformed yoda expression)
if(null==my_variable){}; //:CORRECT
if(my_variable= null){}; //:WRONG#2 (for completeness of example)

Result ( WRONG#1 ):

unexpected type [ERROR]
required: variable [ERROR]
found: value

Result ( WRONG#2 ):

incompatible types: TYPE_OF_MY_VARIABLE_HERE cannot be converted to boolean

Upvotes: 0

Lion
Lion

Reputation: 19027

String myStr = "abcdef";
String reversed = "";

for(int j = myStr.length()-1 ; j >= 0; j--)
{
     reversed += myStr.CharAt(j);
}

Upvotes: 1

ILMTitan
ILMTitan

Reputation: 11017

Variables are places where you can store values. They must be declared and assigned a value before being used:

String reversed = "";

reversed is a variable assigned the value "". Only variables may be on the left side of an assignment (such as =). += is a special type of assignment. a += b; is basically the same as a = a + b;.

Values are the results of expressions. Expressions are things like variable references (reversed), constants (""), method calls (myStr.charAt(0)), and operations (a + b). Values can be on the right side of an assignment.

I'm not sure why it is giving you the correct answer, as the code shouldn't run at all when you have a compile error like that.

Upvotes: 0

Mysticial
Mysticial

Reputation: 471199

The problem is here:

myStr.charAt(j) += reversed;

The left-hand-side is a value. Not a variable. That's why you can't to a += to it.


Although it defeats the purpose of learning how do it the hard way, you can do it like this:

myStr = new StringBuffer(myStr).reverse().toString();

Upvotes: 5

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

+= works on a variable not a value. myStr.charAt(j) returns a value, and you can't use += on that value. It has to be some variable like reversed. e.g.

reversed += myStr.substring(j, j+1);

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

What you want is reversed += myStr.charAt(j). You can only assign a value to a variable. myStr.charAt(j) is not a variable. It's an expression that returns a value or type char.

You should also use a StringBuilder and append each char to the StringBuilder, because your loop creates a new String instance at each iteration, which is not efficient at all.

Upvotes: 0

A4L
A4L

Reputation: 17595

it sould be reversed += new String(myStr.charAt(j)); ... the unexpected type is that what charAt(j) returns

Upvotes: 2

Related Questions