rendered paste body
/*
pa3
Colin Ahern
cahern
program that takes a list of input from a file and determines primality
*/
import java.io.*;
import java.util.Scanner;
class Primes{
public static void main(String[] args){
Scanner sc;
int number,temp=-1,testr;
if(args.length==0){
Usage();
return;
}
for(int i=0;i<args.length;i++){
try{
sc = new Scanner(new File(args[i]));
}catch(FileNotFoundException e){
Usage();
continue;
}
while(sc.hasNextLine()){
while(temp<=0){
if(sc.hasNextInt()){
temp=sc.nextInt();
}else{
sc.next();
}
}
number=temp;
testr=testPrime(number);
if(testr==1) System.out.println(number+" is prime.");
if(testr>1) System.out.println(number+" is divisable by "+testr);
if(testr==0) System.out.println("1 is neither prime nor composite");
temp=-1;
}
}
}
static int testPrime(int n){
int temp;
if(n==1) return 0;
for(int i=2;i*i<=n;i++){
temp=n%i;
if(temp==0){
return i;
}
}
return 1;
}
static void Usage(){
System.out.println("Usage: java Primes input_file");
}
}