-2
else if (!strEmail.toUppercase.charAt(0) < 'A' || !strEmail.toUppercase.charAt(0) > 'Z')
        {
            JOptionPane.showMessageDialog(null, "Must start with A through Z or a through z");
        }

I tried doing that but I kept getting an error message. Should I just use charAt alone? Am I better off using indexOf to check if the string or email starts with a letter lower case or upper. I was thinking combining index and char, but I tried that, and it didn't work.

Drizzy
  • 23
  • 2
  • 3
    `...but I kept getting an error message.` -- And what might that error message be? – azurefrog Feb 21 '17 at 22:58
  • If that code snippet is supposed to be Java, it will not compile. Your errors are syntactical, and don't have anything to do with `charAt()`. `.toUppercase.` should be `.toUpperCase().` – Cᴏʀʏ Feb 21 '17 at 23:00
  • cannot find symbol – Drizzy Feb 21 '17 at 23:00
  • @Drizzy that's not the complete error message. The complete error message tells you **which** wymbold cann't be find, and where the problem is. Why don't you read it? Why don't you even think that reading it might be informative? – JB Nizet Feb 21 '17 at 23:03

3 Answers3

0

You need to change the 'or' to an 'and'- you have to be both lower than a A and greater than a Z. Remember- with ||, only one condition has to be true for the whole statement to be true.

SarTheFirst
  • 350
  • 1
  • 11
0

You can use regex to check whether a String starts with an alphabet, e.g.:

public static void main(String[] args) throws IOException {
    String pattern = "^[a-zA-Z].*$";
    System.out.println("abc".matches(pattern));
    System.out.println("Abc".matches(pattern));
    System.out.println("1ds".matches(pattern));
    System.out.println("1r1".matches(pattern));
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

It would be toUpperCase(); and I would use substring so I that I might test with a regular expression. Like,

if (!strEmail.toUpperCase().substring(0, 1).matches("[A-Z]")) {
    System.out.println("does not match");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249