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:
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;
}
}