This should work. There should be a better way to find out the lowest out of the three indexes greater than -1 instead of putting those in an array and comparing. Please try this code. None of your lines actually begin with TGA. So, you might want to change it before you start testing. I modified the dna file while testing.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DNAApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the full path to the fasta file.");
String input = in.nextLine();
File file = new File(input);
try {
Scanner fileScan = new Scanner(file);
while (fileScan.hasNextLine()) {
String line = fileScan.nextLine();
line = line.replaceAll("X", "A");
if (line.startsWith("ATG")) {
int[] dnaArray = new int[3];
//Get the lowest index out of TAA, TGA, or TAG
dnaArray[0] = line.indexOf("TAA",3);
dnaArray[1] = line.indexOf("TGA",3);
dnaArray[2] = line.indexOf("TAG",3);
//We cannot use Math.min, because it will consider -1 as the lowest if it does not find a string with the index passed to it.
int lowestIndex = Integer.MAX_VALUE;
//We try to find the lowest of all indexes.
for(int index : dnaArray) {
//Check only if the current index is greater than -1 and less than the current lowest index.
if(index > -1 && index < lowestIndex) {
lowestIndex = index;
}
}
//Do a substring from the line only if lowestIndex is not equal to the Integer.MAX_VALUE (intial value set
if(lowestIndex != Integer.MAX_VALUE) {
line = line.substring(0,lowestIndex + 3);
}
System.out.println(line);
}
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
}