简体   繁体   中英

Bool vals won't print out in console

I am interested in a very simple string verification problem to see if the starting character in a string starts with an upper case letter and then have the console to display true or false. From my understanding you wouldn't have to invoke something like System.console().printf("true", s) in order to make this happen. I could swear I've seen similar elementary implementations achieved using the following sample code:

public class Verify {
    public static boolean checkStartChar(String s) {
        if (s.startsWith("[A-Z]")) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        String str = "abCD";
        checkStartChar(str);
    }
}

but when I run this, nothing displays. If I make a slight modification by adding in conditional printouts before returning T/F, eg

public class Verify2 {
    public static boolean checkStartChar(String s) {
        if (s.startsWith("[A-Z]")) {
            System.out.println("yep");
            return true;
        }
        else {
            System.out.println("nope");
            return false;
        }
    }

    public static void main(String[] args) {
        String str = "abCD";
        checkStartChar(str);
    }
}

the issue is somewhat resolved, as the console displays either "yep" or "nope", yet unresolved because I just want the console to display true or false. That's it. Advice?

As the question has already been answered, I'd like to point out there is no need for RegExes to solve this (and they are expensive operations). You could do it simply like this:

static boolean startsWithUpperCase(String toCheck)
{
    if(toCheck != null && !toCheck.isEmpty())
    {
        return Character.isUpperCase(toCheck.charAt(0));
    }
    return false;
}

yet unresolved because I just want the console to display true or false

Calling checkStartChar method will return value, that doesn't mean it will print value to console. You need to code how you would like to handle return value. If you want to print return value, then you should do:

System.out.println(checkStartChar(str));

Will print what ever the return of checkStartChar method

if(s.startsWith("[A-Z]")){

String.startsWith(prefix) doesn't take regex as a parameter, you should be using regex APi instead.

Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(new Character(s.charAt(0)).toString());
if(m.find()){
            return true;
}else{
    return false;
}
String str = "AbCD";
System.out.println(checkStartChar(str));

Output:

  true

In your code checkStartChar(str); is returning a boolean value which is not being used in your program.Then if you want to display true or false then you can use. System.out.println(checkStartChar(str));

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