0

Under for loop i am getting string values like decimal,integer, negative decimal values, i want to convert all of them in integer with one if condition,

how to achieve this?

 for(WebElement rowElement:TotalRowCount)
        {
              List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td/span"));
              int ColumnIndex=1;
              for(WebElement colElement:TotalColumnCount)
              {
                  String s=colElement.getText().replace("%", "");

                  if(!s.isEmpty()&&!s.equals("NA"))
                  {

                   System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+s);

                  }
                  else{/*do nothing*/}
               }
              RowIndex=RowIndex+1;
         }
Dev
  • 6,628
  • 2
  • 25
  • 34
sameer joshi
  • 377
  • 2
  • 8
  • 27
  • 3
    you want to convert decimal numbers to integers? – Stultuske Sep 19 '17 at 06:21
  • 3
    Possible duplicate of [How to convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-to-convert-a-string-to-an-int-in-java) – iamsankalp89 Sep 19 '17 at 06:31
  • i dont know what string contains , it may conatin decimal, or integer, it is in loop i want if condition that should convert to integer – sameer joshi Sep 19 '17 at 07:18

6 Answers6

1
int intnum= Integer.parseInt("1234");

See the Java Documentation for more information.

(If you have it in a StringBuilder (or the ancient StringBuffer), you'll need to do Integer.parseInt(myBuilderOrBuffer.toString()); instead).

Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52
1

If you are expecting decimals, then you can use BigDecimal.

BigDecimal nm = new BigDecimal(s);
int required = nm.setScale(0, RoundingMode.DOWN).intValueExact();
RP-
  • 5,827
  • 2
  • 27
  • 46
1
 for(WebElement rowElement:TotalRowCount)
        {
              List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td/span"));
              int ColumnIndex=1;
              for(WebElement colElement:TotalColumnCount)
              {
                  String s=colElement.getText().replace("%", "");

                  if(!s.isEmpty()&&!s.equals("NA"))
                  {

                   System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+s);
         int val = (int) Double.parseDouble(s); // ==> This is val in integer

                  }
                  else{/*do nothing*/}
               }
              RowIndex=RowIndex+1;
         }
Dev
  • 6,628
  • 2
  • 25
  • 34
  • "_i am getting string values like decimal,integer, negative decimal values_" you will not parse everything with that code – AxelH Sep 19 '17 at 06:34
  • Changed please verify – Dev Sep 19 '17 at 06:40
  • Off course I would mention `java.lang.Math` class to be able to round based on OPs choice, using `Float` would even remove the casting (because `int Math.round(float)` ) ;) – AxelH Sep 19 '17 at 06:47
1

You can check first that if a received String is numeric or not by the following method and put that method call as a condition in your if statement.

public static boolean isNumeric(String s) {
        if (s.isEmpty())
            return false;
        try {
            double d = Double.parseDouble(s);
        }
        catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

So, your If condition in for loop should look something like if(isNumeric(s)){...}. Also, to parse these values only to the Integer you can cast them by Math.floor(Double.parseDouble(s) + 0.5d). I think that should solve the issue.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

Declare it above.

int result = 0;

As per your code ,

String s=colElement.getText().replace("%", "");
result = Integer.parseInt(s);

Now result holds the integer value that you need to check.

FreedomPride
  • 1,098
  • 1
  • 7
  • 30
  • "_i am getting string values like decimal,integer, negative decimal values_" you will not parse everything with that code. – AxelH Sep 19 '17 at 06:36
0

As your input may be decimal,integer, negative try below code to get proper rounded off result:

String s = "-88.4";
int a = Math.round(Float.parseFloat(s));
System.out.println(a);
Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23
  • 1
    you could simply do `Math.round(Float.parseFloat(s))` to remove the casting here, I doubt you would get a difference between `double` and `float`. – AxelH Sep 19 '17 at 06:52