0

Java - Why is there a 'cannot find symbol' error despite having already declared the variable?

Code: https://code.sololearn.com/cq3SmFJGQC2X/#java The error seems to occur on line 27.

15th Nov 2020, 7:31 PM
Solus
Solus - avatar
2 odpowiedzi
+ 4
Try putting the scanner and int number; before the do while loop and inside just say, number = scanner.nextInt();
15th Nov 2020, 7:44 PM
Avinesh
Avinesh - avatar
+ 1
Since you declared and initialized the scanner and number variables inside the do block it can only be accessed in the do block but you are trying to access it in the while statement hence you are getting the error. To fix it just do as Avinesh said. To get more information on this visit: https://www.google.com/amp/s/www.geeksforgeeks.org/variable-scope-in-java/amp/
15th Nov 2020, 8:31 PM
pNK
pNK - avatar