-4

Please give me an idea on how to compare values. I have a string budget_event, and budget and it is from my webserver. I think I should convert such strings into int and I need to compare those values with greater than and less than symbols. How can I implement these? Please give me some ideas. I'm new in these things.

Here's my code snippet in android. I need to convert the Budget and budget_event to int because they are strings. Any help will do. Thanks!

 Budget = jsonObject.getString("budget");
totalcost.setText(Budget);
budget_event = budget.getText().toString();
user3319349
  • 15
  • 1
  • 4

4 Answers4

3

You can use

 int value = Integer.parseInt(Budget);

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

If Budget is not valid int, it throws a NumberFormatException.

  try {
       int value = Integer.parseInt(Budget);
  }catch(NumberFormatException e) {
       e.printStackTrace();
  }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
2

As simple as:

int i = Integer.parseInt(yourString);

Or, for floats:

float f = Float.parseFloat(yourString);
FD_
  • 12,947
  • 4
  • 35
  • 62
0

Pass your string value to parseInt() method of Integer class...you will get the converted value of the given string value.

int budget = Integer.parseInt(budget_event);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
0
int budgetint = Integer.parseInt(budget);
if (budgetint <20)
...
Orphamiel
  • 874
  • 13
  • 22