2017-02-21 36 views
-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"); 
     } 

我想這樣做,但我一直收到錯誤消息。我應該單獨使用charAt嗎?我最好使用indexOf來檢查字符串或電子郵件是否以小寫或大寫字母開頭。我在想結合索引和字符,但我嘗試過,並沒有奏效。如何使用的charAt檢查字符串不具有A-Z或A-Z開始?

+3

'...但我不斷收到一條錯誤消息。「 - 什麼可能是錯誤信息? – azurefrog

+0

如果該代碼片段應該是Java,它將不會編譯。你的錯誤是句法的,並且與'charAt()'沒有任何關係。 '.toUppercase.'應該是'.toUpperCase()。' –

+0

找不到符號 – Drizzy

回答

0

您需要將'或'更改爲'和' - 您必須低於A並且大於Z.請記住 - 使用||時,對於整個語句只有一個條件必須爲真是真實的。

0

您可以使用regex檢查是否String打頭字母,例如:

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

這將是toUpperCase();我會使用substring,以便我可以用正則表達式進行測試。像,

if (!strEmail.toUpperCase().substring(0, 1).matches("[A-Z]")) { 
    System.out.println("does not match"); 
} 
相關問題