Problems compiling

I'm working on a homework assignment where I have to write a program that gets input from a file and then determines whether all the inputs are prime or not. I think I'm on the right track but I'm stuck. When I try and compile it I get this error:

Prime2.java:21: not a statement

for (d = 2, d < Math.sqrt(n); d++;){

^ //this is supposed to be pointing at the '<' but I'm not very good at formatting my posts so I don't know if it will come out correctly.

How is '<' not a statement?

Here is the block of code around it.

while(sc.hasNext()){

if(sc.hasNextInt()){

n = sc.nextInt();

if(n>1)

isPrime= true;

for (d = 2, d < Math.sqrt(n); d++;){

if((n%d) == 0){

System.out.println(n + " is divisible by 2.");

isPrime=false;

break;

}

what am I doing wrong?

[884 byte] By [mrfredmana] at [2007-11-27 5:19:35]
# 1
> Prime2.java:21: not a statement> for (d = 2, d < Math.sqrt(n); d++;){look at the for loop. Is it supposed to have commas or semicolons? What is present in your code?Good luck!/Pete
petes1234a at 2007-7-12 10:43:01 > top of Java-index,Java Essentials,Java Programming...
# 2

import java.util.*;

class Prime{

public static void main( String[] args ){

Scanner sc = null;

int n, d;

boolean isPrime;

if(args.length!=1)

Usage();

try{

sc = new Scanner(new File(args[0]));

}catch(FileNotFoundException e){

System.err.println(e.getMessage());

Usage();

}

while(sc.hasNext()){

if(sc.hasNextInt()){

n = sc.nextInt();

if(n>1)

isPrime= true;

for (d = 2, d < Math.sqrt(n); d++;){

if((n%d) == 0){

System.out.println(n + " is divisible by 2.");

isPrime=false;

break;

}

}

if(isPrime);

System.out.println(n + "is prime.");

}

}

}

And when I try and Compile i get this:

Prime2.java:23: not a statement

for (d = 2, d < Math.sqrt(n); d++;){

^

Prime2.java:35: '}' expected

^

2 errors

and I can't fix either of them. (plus I have no idea if it will even run once compiled)

mrfredmana at 2007-7-12 10:43:01 > top of Java-index,Java Essentials,Java Programming...
# 3

you got incorrect syntax in :

for (d = 2, d < Math.sqrt(n); d++;)

comas aren't accepted in a for loop. Use semicolumns instead like that:

for (int d = 2;d < Math.sqrt(n); d++){

//some code

}

if the variable d wasn't declared before you should add int before it

remove the last semicolumn after d++ it's useless.

good luck!

Ratiusa at 2007-7-12 10:43:01 > top of Java-index,Java Essentials,Java Programming...
# 4

>

> remove the last semicolumn after d++ it's useless.

>

I agree, but it sure is better than this misplaced semicolon example that I saw earlier today:

for (i = 0; i < 100; i++) ;

println("why am I only printing once?");

petes1234a at 2007-7-12 10:43:01 > top of Java-index,Java Essentials,Java Programming...
# 5
Indeed. This one is quite funny.
Ratiusa at 2007-7-12 10:43:01 > top of Java-index,Java Essentials,Java Programming...
# 6
you are using wrong syntax in for loop for (d=2,....... shoulde be repalced with for(d=2;...........Thanks
mysticbalia at 2007-7-12 10:43:01 > top of Java-index,Java Essentials,Java Programming...