-2

I am trying to read a file called ecoli.txt, which contains the DNA sequence for ecoli, and store its contents into a string. I tried to print the string to test my code. However, when I run the program, there is no output. I am still new to java so I am sure there is an error in my code, I just need help finding it.

package codons;
import java.io.*;
public class codons 
{

    public static void main(String[] args) 
    {
        try 
        {
            FileReader codons = new FileReader("codons.txt");
            FileReader filereader = new FileReader("ecoli.txt");
            BufferedReader ecoli = new BufferedReader(filereader);
            StringBuilder dna_string = new StringBuilder();
            String line = ecoli.readLine();
            while(line != null);
            {
                dna_string.append(line);
                line = ecoli.readLine();
            }
            String string = new String(dna_string);
            System.out.println(string);
            ecoli.close();
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }


    }

}

edit:

I was still having trouble getting the program to work the way I wanted it to so I attempted to complete writing the rest of what I wanted in the program and I am still not getting any output. Anyway, this is where I am at now:

package codons;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;
public class codons 
{
    public static void main(String[] args) 
    {
        try 
        {
            FileReader filecodons = new FileReader("codons.txt");
            FileReader filereader = new FileReader("ecoli.txt");
            BufferedReader ecoli = new BufferedReader(filereader);
            StringBuilder dna_sb = new StringBuilder();
            String line = ecoli.readLine();
            while(line != null)
            {
                dna_sb.append(line);
                line = ecoli.readLine();
            }
            String dna_string = new String(dna_sb);
            ecoli.close();
            BufferedReader codons = new BufferedReader(filecodons);
            StringBuilder codon_sb = new StringBuilder();
            String codon = codons.readLine();
            while(codon != null)
            {
                codon_sb.append(codon);
                line = codons.readLine();
            }
            String codon_string = new String(codon_sb);
            codons.close();
            for(int x = 0; x <= codon_sb.length(); x++)
            {
                int count = 0;
                String codon_ss = new String(codon_string.substring(x, x+3));
                for(int i = 0; i <= dna_sb.length(); i++)
                {
                    String dna_ss = new String(dna_string.substring(i, i+3));
                    int result = codon_ss.compareTo(dna_ss);
                    if(result == 0)
                    {
                        count += 1;
                    }
                }
                System.out.print("The codon '");
                System.out.print(codon_ss);
                System.out.print("'is in the dna sequence");
                System.out.print(count);
                System.out.println("times.");
            }
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }


    }

}
Nunzio
  • 1
  • 1
  • 2
    Time to debug -- print out the line within the while loop to see what is going on, print out the path to your file to be sure that it is correct – Hovercraft Full Of Eels Apr 05 '17 at 22:50
  • You should also add one more catch block for just `Exception` in case the exception type isn't `FileNotFoundException` or `IOException`. – Drew Kennedy Apr 05 '17 at 22:53
  • I'm also perplexed as to why you're iterating over `line` when it's initialized as a `String`. There's really no point to that - better off being an `if` instead of `while`. – Drew Kennedy Apr 05 '17 at 22:54
  • voting to close as a typographical/trivial error – Hovercraft Full Of Eels Apr 05 '17 at 23:03
  • @DrewKennedy I want to read multiple lines from the file to one long string. I used a while loop to append the line that was read when the string 'line' was initialized and then read the next line before appending again until there are no more lines. – Nunzio Apr 06 '17 at 00:48
  • @HovercraftFullOfEels I printed the line within the while loop and each line does output as it should but as the program continues, the lines disappear and there is eventually a blank screen again. – Nunzio Apr 06 '17 at 00:53

1 Answers1

4

Remove the ; after while(line != null), it causes an infinite loop instead of executing the next instructions.

The reason is explained here: Effect of semicolon after 'for' loop (the question is about the C language, but it is equivalent in Java).

Community
  • 1
  • 1
Loris Securo
  • 7,538
  • 2
  • 17
  • 28
  • I understand why the semicolon should be removed but there is still no output after I removed it. – Nunzio Apr 06 '17 at 00:42
  • @Nunzio the provided example with a test file works as expected, if you still don't see the output there could be a problem somewhere else or with the input file that you are using – Loris Securo Apr 06 '17 at 16:58
  • I edited my post with an update of where I am now. I still don't have an output. Any feedback would be greatly appreciated. – Nunzio Apr 10 '17 at 01:41