So I have a large text file full of primes, and I want to be able to ask for a single prime, like the 48th prime for example, and I want Java to read that specific line and return the value. As of now, I have this:
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Long> primes = new ArrayList<Long>();
Scanner read = new Scanner(new File("file.txt"));
Scanner input = new Scanner(System.in);
System.out.println("Which prime do you want?");
int in=Integer.valueOf(input.nextLine());
for(int i=0; i < in; i++){
if(read.hasNext()){
long s = Long.valueOf(read.next());
primes.add(s);
}
}
System.out.println("The "+ in + "th prime is: "+primes.get(in-1));
}
I was wondering if there is an easier way to do this, without having to read the entire file up to the input.