Please tell me why it is saying variable declaration is not allowed here but it is running in the actual compiler | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please tell me why it is saying variable declaration is not allowed here but it is running in the actual compiler

import java.util.*; public class Program { public static void main(String[] args) { Scanner in=new Scanner(System.in); String s,str=""; System.out.println("Enter a string"); s=in.nextLine(); s=s.toLowerCase(); s=s.trim(); int l=s.length(); for(int i=97;i<=122;i++) { for(int j=0;j<l;j++) char c=s.charAt(j); /* It is saying that variable declaration of c is not allowed here, but it is actually running in bluej */ if(c==(char)i) str=str+(char)i; } System.out.println("The string when in alphabetical order: "+str); } }

16th Oct 2022, 3:39 AM
五条悟 ♡
五条悟 ♡ - avatar
3 Answers
+ 3
after second for() there are not parentheses, it is same as for (int j=0; j<l; j++) { // implicitly char c = s.charAt(j); } // implicitly if (c==(char)i) str=str+(char)i; in next if() It can't resolve c variable, because it is after end of second for() scope I run it on android and jdoodle web java 17 with same error
16th Oct 2022, 5:40 AM
zemiak
+ 2
zemiak Done ✔✔ https://code.sololearn.com/coRR9tM8nROl/?ref=app Thnk u soooo muchhhh sir 😊
16th Oct 2022, 6:02 AM
五条悟 ♡
五条悟 ♡ - avatar
+ 1
zemiak thnk u for ur answer I am gonna try using parentheses
16th Oct 2022, 5:43 AM
五条悟 ♡
五条悟 ♡ - avatar