JAVAWarningSyntax ErrorMay 8, 2026

Syntax Error

Cannot find symbol: 'result' in the for-each loop

What This Error Means

A Syntax Error occurs when the compiler cannot understand the code. In this case, the error is due to a missing declaration of a variable 'result' within the for-each loop.

Why It Happens

This error typically happens when a developer forgets to declare a variable used within a loop or method. The variable 'result' is used within the for-each loop but is not declared anywhere in the code.

How to Fix It

  1. 1To fix this error, you need to declare the 'result' variable before using it. You can initialize it before the for-each loop or declare it as a local variable within the loop. The corrected code would look like this:
  2. 2// Before (broken code)
  3. 3for (int i = 0; i < array.length; i++) {
  4. 4 int result = array[i];
  5. 5 // do something with result
  6. 6}
  7. 7// After (fixed code)
  8. 8int result;
  9. 9for (int i = 0; i < array.length; i++) {
  10. 10 result = array[i];
  11. 11 // do something with result
  12. 12}

Example Code Solution

❌ Before (problematic code)
Java
String[] values = {"apple", "banana", "cherry"};
for (String value : values) {
  String result = value.toLowerCase();
  System.out.println(result);
}
✅ After (fixed code)
Java
String[] values = {"apple", "banana", "cherry"};
for (String value : values) {
  String result = value.toLowerCase();
  System.out.println(result);
}

Fix for Cannot find symbol: 'result' in the for-each loop

Related JAVA Errors

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error