2012-12-08 223 views
0

中的角色,我有這樣的代碼檢查字符串

if(!(lotNo.charAt(0) >= "0" && (lotNo.charAt(0) <= "7"))) { 
    // if the first character is not within these boundaries 
    return false; 
} 
return true; 

這種方法給我留下了一個錯誤說bad operator type?雖然它應該檢查字符串中的第一個字符是否在0和7之間。我在正確的行嗎?

+0

乾杯大家,我甚至不知道「」和「之間有區別?你們都救了我幾個小時的壓力:) –

回答

1

您正在比較字符串而不是字符。嘗試:

if (!(lotNo.charAt(0) >= '0' && (lotNo.charAt(0) <= '7'))) { // if the first character is not within these boundaries 
    return false; 
} 
return true; 

"0" < - 字符串具有一個char,'0'
'0' < - 字符,字符'0'

2

Char必須在'0'"0"。第二個是String

0

charAt(int index)方法的返回值類型是char。 但你正在比較它與字符串,這就是爲什麼你得到bad operator type