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]

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)
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!
>
> 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?");