0

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, e.g.

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?

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
OcelotXL
  • 159
  • 2
  • 6
  • 14

4 Answers4

5

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;
}
MrLore
  • 3,759
  • 2
  • 28
  • 36
4

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

kosa
  • 65,990
  • 13
  • 130
  • 167
  • ok that works out awesomely; just added it to main and I was good to go. Thanks – OcelotXL Jan 18 '13 at 21:46
  • @madrush: You are welcome. Good luck. Consider using regex and other solutions suggested if useful (I am not regex expert). – kosa Jan 18 '13 at 21:47
1
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
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • 1
    you can also use the simpler `String.matches(String regex)` method on the string you are trying to match. Note that for matches to return `true` it has to match the full string, so in that case the regex would be `^[A-Z].*` – Peter Elliott Jan 18 '13 at 21:21
  • @PeterElliott absolutely, but tbh i am not really good with regex, as you can see, i used a very basic regex. but thanks for the suggest... :) – PermGenError Jan 18 '13 at 21:22
0

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));

Tushar Paliwal
  • 301
  • 4
  • 11