1

The Problem I have to do is as follows

public static String randomDNAString(int dnaLength) /** * @param dnaLength a non-negative int * @return a random String of length dnaLength comprised of the four chars A, T, G, and C */

I thought I did it correctly (though incredibly inefficiently) but it doesn't return anything. Here's my code. Any help is appreciated.

    public static String randomDNAString(int dnaLength){
    Random rand = new Random();
    char[] dna;
    dna = new char[dnaLength];
    for(int i = 0; i<dnaLength;i++){
        int tempC = rand.nextInt(4);
        if(tempC == 0)
            dna[i] = 'A';
        if(tempC == 1)
            dna[i] = 'G';
        if(tempC == 2)
            dna[i] = 'C';
        if(tempC == 3)
            dna[i] = 'T';

    }
    return (java.util.Arrays.toString(dna));
}
6
  • 2
    What does it return? This seems to be fine. Commented Apr 25, 2014 at 0:48
  • It's supposed to return a String. But when I run it, nothing gets returned Commented Apr 25, 2014 at 0:49
  • 1
    @Jonerhan I ran it. It works. [A, G, G, G, A, G, G, C, A, A] Commented Apr 25, 2014 at 0:49
  • Interesting. I'm not getting an error, but the console doesn't return anything either Commented Apr 25, 2014 at 0:51
  • 1
    Why do you expect the console to show anything? None of your code that you've shown here has a print() call. Where is your main() method? What does it do? Commented Apr 25, 2014 at 0:52

3 Answers 3

2

The other answer's suggestion using of using StringBuilder was a good one. However, you can make this even simpler:

public static String randomDNAString(int dnaLength) {
    Random rand = new Random();
    StringBuilder dna = new StringBuilder(dnaLength);

    for (int i = 0; i < dnaLength; i++) {
        dna.append("ACGT".charAt(rand.nextInt(4)));
    }

    return dna.toString();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one! This is a lot smarter than my derpy switch statement :)
0

We can improve on your answer by changing the if chain to a switch statement and using a StringBuilder.

public static String randomDNAString(int dnaLength) {
    Random rand = new Random();
    StringBuilder dna = new StringBuilder();
    for(int i=0;i<dnaLength;i++) {
        switch(rand.nextInt(4)) {
            case 0:
                dna.append("A");
                break;
            case 1:
                dna.append("C");
                break;
            case 2:
                dna.append("G");
                break;
            case 3:
                dna.append("T");
                break;
        }
    }
    return dna.toString();
}

To print the output, try:

System.out.println(randomDNAString(10));

Comments

0

There's nothing wrong with this. I'm just dumb and when I called it in the main I used

randomDNAString(20);

instead of

System.out.println(randomDNAString(20));

Thanks for your help everyone, I'm just dumb sometimes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.