2016-03-02 14 views
1

我很困惑。我想equals我的hourRate只有整數。我該怎麼辦?我想'等於'我的`HourRate`只有整數

JTextField hourRate = new JTextField(20); 
if (!hourRate.getText().equals(string)) 

對不起,我的英文不好

+1

http://stackoverflow.com/questions/5585779/converting-string-to-int-in- java –

+1

開始使用Java命名約定,以便其他人可以讀取您的代碼。變量名(例如'hourRate'和'string'而不是'String')應該以*小寫*字母開頭。類名稱(如'JTextField')以*大寫*字母開頭。如果你這樣做,你還會發現StackOverflow上的語法着色突然正常工作。 –

回答

4

也這樣做,

try { 
    JTextField HourRate = new JTextField(20); 

    // Convert string integer value to integer 
    Integer hourRateInInt = Integer.parseInt(HourRate.getText().trim()); 

    if (hourRateInInt != someValue) 
} catch (NumberFormatException ne) { 
    // Error in string parsing to integer 
} 
0

試試這個

要比較你的HourRate爲整數,然後你必須首先轉換您的用戶輸入值整數然後你可以比較你的HourRate和另一個intvalue。

嘗試{

JTextField HourRate = new JTextField(20); 

    int hourRate =Integer.parseInt(HourRate.getText()); 

    //parseInt() throw NumberFormatException if user not input valid value 

    if (hourRate == integerValue) //use == Operator for integer comparison 
    { 
    //equal value 
    } 
    else{ 
    //not equal value 
    } 

}趕上(NumberFormatException的NE){

}

相關問題