1

I am confused. I want to equals my hourRate into integer only. What should I do?

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

Sorry for bad english

Jan Vladimir Mostert
  • 12,380
  • 15
  • 80
  • 137
User9124
  • 41
  • 1
  • 1
    http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java – akash Mar 02 '16 at 12:40
  • 1
    Start using the Java naming convention, so other people can read your code. Variable names (like `hourRate` and `string` instead of `String`) should start with a *lower-case* letter. Class names (like `JTextField`) start with an *upper-case* letter. If you do that, you'll also find that the syntax coloring on StackOverflow suddenly works correctly. – Erwin Bolwidt Mar 02 '16 at 12:54

2 Answers2

4

do likewise,

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 
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
0

Try this

You want to compare your HourRate to integer then you must first convert your User Input value to integer and then you can compare your HourRate to another intvalue.

try {

    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
    }

}catch (NumberFormatException ne) {

}

Pravin Fofariya
  • 346
  • 2
  • 11