简体   繁体   中英

How to use a method with a char input and a boolean output?

Thank you everyone for your answers ! One more question: how do i print out the boolean value? System.out.println(goodBase) does not seem to work

public class Dna {
      public static void main(String[] args){
      aBase('A');
     }

      public static boolean aBase (char c) {                 
        char [] charArray = { 'A', 'G', 'C', 'T' };
        boolean goodBase;

        if (c == 'A' || c == 'G' || c == 'C' || c == 'T') 
        {
          return true;     
        } 
        else 
        {
          return false;
        }
      }  
    }

Thanks !

The code is working for me if you use it in the right environment. I have attached a complete functioning sample below:

public class Main {

    // create a Test Method
    public static boolean test(char c) {
        if (c == 'A' || c == 'G' || c == 'C' || c == 'T') {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {

        // create some sample data
        String sample = "AGCTEDHI";

        // test
        for (int i = 0; i < sample.length(); i++) {
            char current = sample.charAt(i);
            System.out.println(current + " is " + test(current));
        }

    }
}

Output:

A is true
G is true
C is true
T is true
E is false
D is false
H is false
I is false

I think that is so:

char [] charArray = { 'A', 'G', 'C', 'T' };
if (base == charArray[0] || base == charArray[1] || 
        base == charArray[2] || base == charArray[3])
  return true; // a good base
return false;

First of all, there is no declaration of a char named variable in the aGoodBase method you are checking in the if statement.

And secondly, and this is the real deal, such variable could never exist since char is a reserved keyword in Java (like byte , int , long , public , strictfp etc.) , and it can be used only to declare a variable as a primitive char type.

This answers why DnaTest is not compiling.

Now, let's consider a possible solution for your answer:

    public class DnaTest {

        private final static char [] baseArray = {'A', 'C', 'G', 'T'};

        // checks whether String s only contains the right base elements or not
        public static boolean aGoodBase(String s){
                String s1 = s.toUpperCase(); // just ensuring case insensitive checks
                for (int i=0; i<s1.length(); i++) {
                        char check = s1.charAt(i);
                        if (baseArray[0] != check &&
                              baseArray[1] != check &&
                              baseArray[2] != check &&
                              baseArray[3] != check) {
                                   // at least one char of s is not in baseArray
                                   return false;
                                }
                }
                return true;
        }

        public static void main(String[] args) {
                // should be true
                System.out.println("Does GATTACA have aGoodBase? " + aGoodBase("GATTACA"));
                // should be false
                System.out.println("Does MORROW have aGoodBase? " + aGoodBase("MORROW"));
                // should be true, the check on the base is case insensitive
                System.out.println("Does GaTTaca have aGoodBase? " + aGoodBase("GaTTaca"));
                // should be false
                System.out.println("Does GATMOR have aGoodBase? " + aGoodBase("GATMOR"));

        }
    }

Output:

Does GATTACA have aGoodBase? true
Does MORROW have aGoodBase? false
Does GaTTaca have aGoodBase? true
Does GATMOR have aGoodBase? false

Now aGoodBase method takes a String and returns true if s contains only a combination of the four basic elements declared in the baseArray array. I've taken the liberty to return true even when the base elements are not in capital letters (as in the third example of the main: GaTTaca).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM