Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that Scans the DNA sequence filename from the standard input (do

ID: 3889302 • Letter: W

Question

Write a program that

Scans the DNA sequence filename from the standard input (don't print a prompt to the user). Note that the file sent will be an altered version of the file you saved above.

Your project must have a class named Main in a package named ecolicounts.

Reads in the file and prints out the number of A's, C's, G's, and T's in the sequence. The format should look like

#A = 1142136
#C = 1179433
#G = 1176775
#T = 1140877

I have this written down but G and T are switched and need to be like the above problem

public class Main {

public static void main(String args[]) {

try {

HashMap<Character, Long> characterCountMap = new HashMap<Character, Long>();

Scanner inputScanner = new Scanner(in);

String fileName = inputScanner.next();

Scanner scanner = new Scanner(new File(fileName));

while (scanner.hasNext()) {

String s = scanner.next();

for (int index = 0; index < s.length(); index++) {

Character ch = s.charAt(index);

if (characterCountMap.get(ch) == null)

characterCountMap.put(ch, 0l);

characterCountMap.put(ch, characterCountMap.get(ch) + 1);

}

}

for (Character ch : characterCountMap.keySet()) {

out.println("#" + ch + " = " + characterCountMap.get(ch));

}

} catch (IOException e) {

out.println(e.getMessage());

}

}

}

Explanation / Answer

I found this code working well.

import java.io.File;

import java.io.IOException;
import java.util.Scanner;
import java.util.TreeMap;

public class CountATGC {

    public static void main(String args[]) {

        try {

            TreeMap<Character, Long> characterCountMap = new TreeMap<>();

            Scanner inputScanner = new Scanner(System.in);

            String fileName = inputScanner.next();

            Scanner scanner = new Scanner(new File(fileName));

            while (scanner.hasNext()) {

                String s = scanner.next();

                for (int index = 0; index < s.length(); index++) {

                    Character ch = s.charAt(index);

                    if (characterCountMap.get(ch) == null) {
                        characterCountMap.put(ch, 0l);
                    }

                    characterCountMap.put(ch, characterCountMap.get(ch) + 1);

                }

            }

            for (Character ch : characterCountMap.keySet()) {

                System.out.println("#" + ch + " = " + characterCountMap.get(ch));

            }

        } catch (IOException e) {

            System.out.println(e.getMessage());

        }

    }

}

This will sort the output in alphabetical order!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote