Results 1 to 5 of 5

Thread: Help!!!

  1. #1
    ErikD99 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Question Help!!!

    I am trying to get this code to ouput me the running total for "A" "C" "G" and "T". but it just outputs the title of the file I input. Need help!!!

    Here is the code:

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    import javax.swing.JFileChooser;
    
    // name of class.
    
    public class QA3Q2 {
    
        private static Scanner scanner;
        private static File fastaFile;
        private static JFileChooser chooser;
        private static StringBuilder sb;
        private static int count;
    
        public static void main(String[] args) {
    
            getFile();
            if (fastaFile != null) {
    			// print message and get next input
                System.out.printf("%1$s\r\n", getHeader(fastaFile));
              
            } else {
                System.out.println("Invalid FASTA file.");
            }
        }
    
        public static File getFile() {
    		// show the open file dialog
            chooser = new JFileChooser();
    	// check to see that the user clicked the OK button
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
    		// get the selected file
                fastaFile = chooser.getSelectedFile();
                return fastaFile;
            }
    
            return null;
        }
    
        public static String getHeader(File file) {
            if (file != null) {
                try {
                    scanner = new Scanner(file);
                    // your code working with fileReader goes here
                    return scanner.nextLine().substring(1,
                            scanner.nextLine().length() - 1);
                } catch (FileNotFoundException fnf) {
                    fnf.printStackTrace();
                } finally {
                    if (scanner != null) {
                        scanner.close();
                    }
                }
            }
    
            return null;
        }
    	//the getSequence method should skip the first line contained in fastaFile.
        public static String getSequence(File file) {
            if (file != null) {
                try {
                    sb = new StringBuilder();
                    scanner = new Scanner(file);
    			// your code working with fileReader goes here.
                    while (scanner.hasNextLine()) {
                        if (!scanner.nextLine().contains(">")) {
                            sb.append(scanner.nextLine());
                        }
                    }
    
                    return sb.toString();
                } catch (FileNotFoundException fnf) {
                    fnf.printStackTrace();
                } finally {
                    if (scanner != null) {
                        scanner.close();
                    }
                }
            }
    
            return null;
        }
    	//accepts sequence data (a String containing only As, Cs, Gs, and Ts),
        public static int countBase(String sequence, char base) {
    
            int aTotal= 0;
            int cTotal= 0;
            int gTotal= 0;
            int tTotal= 0;
            for (int i = 0; i < sequence.length(); i++) {
                if (sequence.charAt(i) == 'A') {
                    aTotal= aTotal+1;
                }
                else if (sequence.charAt(i) == 'C') {
                   cTotal= cTotal+1;
    			}
    			else if (sequence.charAt(i) == 'G') {
                    gTotal= gTotal+1;
    			}
    			else if (sequence.charAt(i) == 'T') {
                    tTotal= tTotal+1;
    			}
            }
    
            return count;
        }
    
    }

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I am trying to get this code to ouput me the running total for "A" "C" "G" and "T". but it just outputs the title of the file I input. Need help!!!
    what was it first line of the file you choose?


    Java Code:
        public static void main(String[] args) {
    
            getFile();
            if (fastaFile != null) {
    			// print message and get next input
                System.out.printf("%1$s\r\n", getHeader(fastaFile));
    
            } else {
                System.out.println("Invalid FASTA file.");
            }
        }
    You only call getHeader(File file) method, so ONLY getHeader(File file) will process. getSequence(File file) and countBase(String sequence, char base) was never called or used in main or in another method. Only the header(the first line of file) will print.


    Java Code:
        public static String getHeader(File file) {
            if (file != null) {
                try {
                    scanner = new Scanner(file);
                    // your code working with fileReader goes here
                     return [b]scanner.nextLine()[/b].substring(1,
                            [b]scanner.nextLine()[/b].length() - 1);
    
                } catch (FileNotFoundException fnf) {
                    fnf.printStackTrace();
                } finally {
                    if (scanner != null) {
                        scanner.close();
                    }
                }
            }
    
            return null;
        }
    At your getHeader(File file) method you call nextLine() twice. Example your File content looks like this
    Java Code:
    HEADER OF THE FILE      	       //this will be the first line. First line has a length of 18.
    >AACCGGTTTTTGGGCCCAAA      	       //This will be the second line, it has a length of 21.
    It will print the first line but the length that will return is the second line's length (21) which will give you error because first line's length is 18 only.
    Last edited by mine0926; 11-17-2010 at 02:06 AM.

  3. #3
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    What do you mean by 'it just outputs the title'? Do you know which part of your code outputs the title? Add some System.out.println statements to debug.

    One point of concern though is your use of scanner.nextLine. This method reads a line from the scanner and after that, the line that has been read is no longer available in the scanner. So a subsequent call to nextLine will read the next available line. Take this in mind and re-read your code.

    Erik
    I'm new to Java but I like to help where ever I can. :)

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Java Code:
        private static Scanner scanner;
        private static File fastaFile;
        private static JFileChooser chooser;
        private static StringBuilder sb;
        private static int count;
    
        ...
        ...
    
        public static File getFile() {
        ...
    
        public static String getSequence(File file) {
        ...
    
        public static int countBase(String sequence, char base) {
        ...
    You want your methods to return something but do not use the returning values and uses the Objects you
    declared outside the methods instead.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •