0

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!

shmosel
  • 49,289
  • 6
  • 73
  • 138

1 Answers1

4

Just Modified few lines,

Changes

1.) Commented Scanner lineScan = new Scanner(line);

2.) Need to reset value for isProtein, in loop, for next iteration.

o/p is printed in text file hi.txt. BTW i have used text file for R/W operations so commented out scanner part.

Code

public static void Results() 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";
                   }
                }else{
                      isProtein = "NO";
                }
            //}
            outputRead.println(isProtein);
            isProtein = "NO";
        }
        //System.out.println(isProtein);
    }

Output

NO
YES
NO
YES
NO
NO
NO
YES
NO
YES
NO
YES
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116