+ 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); } }
3 Antworten
+ 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
+ 2
zemiak Done ✔✔
https://code.sololearn.com/coRR9tM8nROl/?ref=app
Thnk u soooo muchhhh sir 😊
+ 1
zemiak thnk u for ur answer I am gonna try using parentheses