Start New Discussion within our Software Development Community

Hello, I'm just starting to learn Java, and for a summation program that we started in class, I've been stuck on the question as to why this brings up an error message when I try to build it. Please advise me.

import java.util.Scanner;

public class summation{
	public static void main(String[] args){
		int n;
		int s=0;
		Scanner X=new Scanner(System.in);
		System.out.print("Enter # of terms: ");
		n = X.nextInt();
			for(int i=1; i <= n; i=i+2);
				{
				s= s+ i;
				}
		System.out.print(+s);
	}
}

The exact probem shows that within the for loop, the i is not found.

for(int i=1; i <= n; i=i+2);
				{
				s= s+ i;
                                      ^
				}

@NexG-- this is because you are putting a semi-colon at the end of for loop. What you have written is

for(int i=1; i <= n; i=i+2);

It should have been

for(int i=1; i <= n; i=i+2)

i.e without semi-colon.So this was the correction from SYNTAX-POINT-OF-VIEW but i think the summation answer will still come wrong since you are incrementing "i" by 2 so i think you should increment it by 1 to get correct answer. So finally for loop will be

for(int i=1; i <= n; i=i+1)
This question has already been answered. Start a new discussion instead.