I'm trying to print out "YES" for each line in a file (dna.txt
) that starts with "ATG"
and ends with "TAA"
, "TAG"
, or "TGA"
, and "NO" when this isn't the case. It should stop after the lines in the file are done, but I've created some kind of loop in my code where nothing is printed to the output file (hi.txt
) but "NO"...endlessly. I know it should have some "YES"ses too, but my problem is clearly larger than just not reading the tokens of the file correctly.
My code:
public static void Results(Scanner console) throws
FileNotFoundException {
System.out.print("Input file name? ");
Scanner input = new Scanner(new File("dna.txt"));
System.out.print("Output file name: ");
File outputFile = new File("hi.txt");
System.out.println();
PrintStream outputRead = new PrintStream(outputFile);
String isProtein = "NO";
while (input.hasNextLine()) {
String line = input.nextLine().toUpperCase();
Scanner lineScan = new Scanner(line);
while (lineScan.hasNext()) {
if (line.startsWith("ATG")) {
if (line.endsWith("TAA") || line.endsWith("TAG") ||
line.endsWith("TGA")) {
isProtein = "YES";
}
}
}
outputRead.println(isProtein);
}
System.out.println(isProtein);
}
Text file (though it should work with any text file, and it isn't):
protein?
ATGCCACTATGGTAG
protein?
ATgCCAACATGgATGCCcGATAtGGATTgA
protein?
CCATt-AATgATCa-CAGTt
protein?
ATgAG-ATC-CgtgatGTGgg-aT-CCTa-CT-CATTaa
protein?
AtgC-CaacaTGGATGCCCTAAG-ATAtgGATTagtgA
protein?
atgataattagttttaatatcaga-ctgtaa
Do you have any idea where this loop is forming? If so, please just give me hints as to how I should fix this.
Thanks!