0

I need to compare char values with set char values 'g' 'c' 'a' 't'(lower and upper case), for i want only those values to be entered. I can not seem to get certain cases of my input validation working.

f in the below strings can stand for any length of string that is not characters g,c,a,t.

The string "fffffff" keeps in the loop. The string "fgf" keeps in the loop.

However, i want the strings, "fffffg" or "gfg" to exit the loop, and they are not doing so.

The actual purpose of the exercise, to take a user input of nucleotides like g,c,a,t like the one's in DNA, and convert them into the complementary string of RNA. G is complement to C and vice versa. A is complement to U(the T is replaced with U) and vice versa. So if the string is "gcat", the response for RNA should be "cgua".

import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;

//getting my feet wet, 1/13/2015, program is to take a strand of nucleotides, G C  A   T, for DNA and give
//the complementary RNA strand, C G  U A.
public class practiceSixty {

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
     public void run() {

        String input = null;

        boolean loopControl = true;

        char nucleotide;

        while(loopControl == true)
        {
            input = JOptionPane.showInputDialog(null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
            for(int i = 0; i < input.length(); i++)
            {
            nucleotide = input.charAt(i);

                if(!(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' ))
                {
                 loopControl = true;
                }
                else if(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' )
                {
                loopControl = false;
                System.out.println(nucleotide);
                }
            }

         }
            JOptionPane.showMessageDialog(null, "the data you entered is " + input);

            StringBuilder dna = new StringBuilder(input);

            for(int i = 0; i < input.length(); i++)
            {
            nucleotide = input.charAt(i);

                if(nucleotide == 'G' || nucleotide == 'g' )
                {
                    dna.setCharAt(i, 'c');
                }
                else if( nucleotide == 'C' || nucleotide == 'c')
                {
                    dna.setCharAt(i, 'g');
                }
                if(nucleotide == 'A' || nucleotide == 'a')
                {
                    dna.setCharAt(i, 'u');
                }
                else if(nucleotide == 'T' || nucleotide == 't')
                {
                    dna.setCharAt(i, 'a');
                }
            }
             JOptionPane.showMessageDialog(null, "the DNA is  , " + input + "  the RNA is  " + dna);
 }
 });
 }
 }

1 Answer 1

1

You could do your check with a single regular expression, and then just use a do/while loop to keep prompting for input until the user enters something valid.

do {
    input = JOptionPane.showInputDialog(
        null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
} while (!input.matches("[GCATgcat]+"));

The regular expression will match any input that consists of one or more letters of the 8 shown. When you don't get a match, the loop repeats.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you David Wallace. I was wary of using Regular expressions, but they seem so useful, and my book had not really introduced me to them.

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.