HOWTO · Java
How to Fix the Unexpected Type Error in Java
Fix Java's unexpected type compiler error by assigning to a variable instead of a method result such as charAt(i). See the required: variable / found: value clue and two string-reversal approaches.
On this page
In Java, an assignment target must be a variable. The charAt(i) method returns a character value, so an expression such as DemoStr.charAt(i) cannot appear on the left side of an assignment. Assign or append that returned character to a variable instead.
Understanding the unexpected type Compiler Error
The unexpected type diagnostic is about what the expression can do in that position. In the example below, charAt(i) produces a value; it does not name a variable whose contents can be changed. The compiler wording required: variable and found: value points to this assignment-target problem, rather than a mismatch between two declared data types. A type mismatch is a different issue: it occurs when an expression has a type that cannot be used where another type is expected. Here, the clue is the kind of thing required on the left side of +=. Changing a cast or a declared type does not make a method result assignable. Find the expression immediately to the left of the assignment operator and check whether it names storage that can be updated.
Reproduce the Error with an Invalid Assignment
The failing operation in the string-reversal example is DemoStr.charAt(i) += ReversedString;. It tries to update the character returned by charAt, but that result is only a value. A compiler diagnostic for this assignment identifies the left side as requiring a variable and finding a value. Compiler formatting can vary, so this description is not a captured compiler transcript. The += operator still needs a writable target, just as a plain = assignment does. A method call can be used to read a character, compare it, or pass it to another operation, but the returned character is not a named slot in the original string. Java strings are immutable, so the intended operation here is to build a separate result rather than modify a character inside DemoStr.
Fix the Assignment to a Variable
Keep the destination on the left and append each character returned by charAt(i) to it. This complete example traverses the source from its last index to its first. Starting at length() - 1 selects the final character; decrementing i visits each earlier index until zero. That order builds the result backward while leaving the source unchanged. If the input string is empty, the initial index is -1, so the loop body runs zero times and the program prints an empty line.
Run this example:
public class AssignmentFix {
public static void main(String[] args) {
String DemoStr = "delftstack";
String ReversedString = "";
for (int i = DemoStr.length() - 1; i >= 0; i--) {
ReversedString += DemoStr.charAt(i);
}
System.out.println(ReversedString);
}
}
Output:
kcatstfled
The key correction is that the assignment changes ReversedString; charAt(i) supplies the character to append. For larger strings, repeated concatenation can create many intermediate strings; the loop is useful for making the assignment-target correction clear, while StringBuilder is usually a better fit when accumulating many characters.
Reverse a String with StringBuilder
For a shorter reversal, construct a StringBuilder from the source, call reverse(), and convert the result back to a String. This is an alternative way to perform the reversal; it is not required to fix the assignment-target error.
public class BuilderReverse {
public static void main(String[] args) {
String DemoStr = "delftstack";
String ReversedString = new StringBuilder(DemoStr).reverse().toString();
System.out.println(ReversedString);
}
}
Output:
kcatstfled
In both examples, the value returned by charAt(i) can be read or appended, while assignment must target a variable. StringBuilder.reverse() performs the reversal on the builder, and toString() gives the resulting text as an ordinary String. Choose the loop when you want to see how characters are accumulated or need to add custom processing; choose the builder form when reversal alone is the goal. Neither approach assigns to a method result.