• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print even numbers from 1 to 20 (error)  RSS feed

 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, i am new to java. I am trying to execute a program to print even number from 1 to 20.



I am getting 6 errors while compiling the program

4 errors

D:\Amit\Java\New folder>javac Even.java
Even.java:6: error: '.class' expected
                for(int x=1;int x<=20;x++)
                                ^
Even.java:6: error: illegal start of expression
                for(int x=1;int x<=20;x++)
                                 ^
Even.java:6: error: ')' expected
                for(int x=1;int x<=20;x++)
                                   ^
Even.java:6: error: ';' expected
                for(int x=1;int x<=20;x++)
                                         ^
4 errors
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Your loop variable x has already been declared as an int in the loop initialization expression; you shouldn't put the type again in the loop condition.
 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply junilu. I am new to Java, it's difficult for me to understand even after your reply. Can you edit the code for me, to get a clear picture.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Raj Ps wrote:Can you edit the code for me, to get a clear picture.

Sorry, but I'm not going to edit it for you. We don't usually do people's work for them around here.  Look up the Java Tutorial section about the for-loop: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

The tutorials refer to what I called "the loop condition" as the termination expression.  You shouldn't declare a type in that part of the for-loop header. Read the documentation and understand what you did wrong by comparing the examples there with what you did. The compiler errors also try to point out the part of the statement that is a problem. Take a hint from the compiler as well.
 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Amit Raj Ps wrote:Can you edit the code for me, to get a clear picture.

Sorry, but I'm not going to edit it for you. We don't usually do people's work for them around here.  Look up the Java Tutorial section about the for-loop: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

The tutorials refer to what I called "the loop condition" as the termination expression.  You shouldn't declare a type in that part of the for-loop header. Read the documentation and understand what you did wrong by comparing the examples there with what you did. The compiler errors also try to point out the part of the statement that is a problem. Take a hint from the compiler as well.


Thanks for the info. Didn't know how the forum works around here. I have been correcting the above code for hours.
I will take a look, hope I can crack it. If not may be Java is not for me
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Raj Ps wrote:
Thanks for the info. Didn't know how the forum works around here. I have been correcting the above code for hours.
I will take a look, hope I can crack it. If not may be Java is not for me

Its not difficult.
Let's see what Junilu said
Junilu Lacar wrote:Your loop variable x has already been declared as an int in the loop initialization expression; you shouldn't put the type again in the loop condition.


Have they taught you what 'declaring' means?
Consider the following

Here, the name and age are declared as variables

As per the JLS you cannot have the same variable declared multiple times.
Now can you see what's wrong with your code

 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, i fixed the code. Thanks Junilu Lacar and Maneesh Godbole.




output:

D:\Amit\Java\New folder>java Even
################################
2
4
6
8
10
12
14
16
18
20
#################################
 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Amit Raj Ps wrote:
Thanks for the info. Didn't know how the forum works around here. I have been correcting the above code for hours.
I will take a look, hope I can crack it. If not may be Java is not for me

Its not difficult.
Let's see what Junilu said
Junilu Lacar wrote:Your loop variable x has already been declared as an int in the loop initialization expression; you shouldn't put the type again in the loop condition.


Have they taught you what 'declaring' means?
Consider the following

Here, the name and age are declared as variables

As per the JLS you cannot have the same variable declared multiple times.
Now can you see what's wrong with your code



Thanks for the explanation, if it wasn't for you guys  needed to wait for a whole day to speak to my trainer. Declaration , initialization has been taught to me, however i am always confused with Loops ( can i call it closure?).

Thanks again for the explanation really helped a a lot. There will be many more doubts to come from my side in future
 
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the working code for you



Regards
Anurag
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem as already mentioned was that you were declaring an already declared variable in your for loop.
Regards
Anurag
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anurag Singh wrote:Here is the working code for you...

Please don't provide complete solutions.  Instead, guide the user to the correct answer.
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Anurag Singh wrote:Here is the working code for you...

Please don't provide complete solutions.  Instead, guide the user to the correct answer.


Ok, I will take care of this in the future.
Regards
Anurag
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!