// find the first prime in a file of numbers import java.io.*; // for File import java.util.*; // for Scanner public class FirstPrime { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("integers.txt")); int first = scanForPrime(input); if(first==0) { System.out.println("No primes were found."); } else { System.out.println("First prime was " + first + "."); } } public static int scanForPrime(Scanner s) { // scan for the first prime and return it while (s.hasNextInt()) { // check if it's prime int n = s.nextInt(); if (isPrime(n)) { return n; } } return 0; } public static boolean isPrime(int n) { for(int i=2; i*i <= n; ++i) { if(n % i == 0) { return false; } } return true; } }