Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking characters of a string using boolean method of the same class
    primarykey
    data
    text
    <p>This is one programming task i'm working toward to. I'm currently struggling with the main method of the code. Supposedly, what i am trying to do is to use the isDNASequence method to check the individual characters in a string to see if it is within the requirement, while also counting the amount of space between the input (for example, if the args input are aaaa gggg cccc tttt acgt accg atgc gcta tccg &lt;= there should be 9 spaces) to make an a 2d array from it. I planned on using a for loop first to work with the isDNASequence method,b ut right now for some reason the charAt method doesn't work and i'm stucked at this part of the code. So any pointers or helps are really appreciated. I'm a really big java noob so pardon me if i ask anything stupid :D </p> <pre><code>import java.util.ArrayList; import java.util.List; public class Hamming { public static boolean isDNASequence(String s) { // create an array of acceptable characters char[] acceptableInput = { 'a', 'c', 'g', 't' }; // render the original string s to lower case into string x String x = s.toLowerCase(); // for loop to check if all the characters are acceptable characters or // not for (int i = 0; i &lt; x.length(); i++) { boolean isDNASequence = false; for (int j = 0; j &lt; acceptableInput.length; j++) { if (x.charAt(i) == acceptableInput[j]) isDNASequence = true; } // the output of the boolean if (isDNASequence == false) return false; } return true; } public static int[][] getDistances(String[] sequences) { // filter out the data and get the number of strings left List&lt;String&gt; validStrings = new ArrayList&lt;&gt;(); for (int x = 0; x &lt; sequences.length; x++) { if (isDNASequence(sequences[x])) { validStrings.add(sequences[x]); } } // create the 2D array int size = validStrings.size(); int[][] result = new int[size][]; for (int i = 0; i &lt; size; i++) { result[i] = new int[size]; } // putting the values from getHammingDistance method into the array cell for (int z = 0; z &lt; result.length; z++) { for (int j = 0; j &lt; result[0].length; j++) { result[z][j] = getHammingDistance(sequences[z], sequences[j]); } } return result; } public static int getHammingDistance(String sequence1, String sequence2) { // Initialise int a, which will be the Hamming distance int a = 0; /** * renders the two strings sequence1 and sequence2 into lower case to * make it easier to calculate */ String sequenceX = sequence1.toLowerCase(); String sequenceY = sequence2.toLowerCase(); /** * I use the for-loop to calculate the Hamming distance based on * comparing individual characters of the two strings */ for (int x = 0; x &lt; sequenceX.length(); x++) { if (sequenceX.charAt(x) != sequenceY.charAt(x)) { a += 1; } } return a; } /** * Main method * * 1. Go through the parameters 2. Ensure they are valid DNA sequences 3. * Get the Hamming distance matrix 4. Print out the highest distance only * * @param args * - program arguments */ public static void main(String[] args) { int d = 0; for (int q = 0; q &lt; args.length; q++) { isDNASequence(args.charAt[q]); } if (args.charAt[q] == " "){ d+=1; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload