sameer joshi
sameer joshi

Reputation: 377

how to know celltype for cell value in jxl?

i want to know the type[double, integer, Number] of cell value,depends on this if it is decimal i want to round up that decimal value. how to achieve that? Please help

Code:

data1 = sh1.getCell(col, row).getContents();
ExcelCell exeCell=new ExcelCell(row, col, data1);
for(ExcelCell x:list1){
            for(ExcelCell y:list2){

        if(x.rowNum==y.rowNum && x.colNum==y.colNum)// && x.cellValue!=null && y.cellValue!=null)
                    {

            if(x.cellValue.matches(y.cellValue)){
                        System.out.println("X:"+x.cellValue+ "  Y:"+y.cellValue);
                    list3.add(new ExcelCell(x.rowNum, x.colNum, "MATCH" ));
                    }
                    else {
                        System.out.println("X:"+x.cellValue+ "  Y:"+y.cellValue);
                        list3.add(new ExcelCell(x.rowNum, x.colNum, "MIS-MATCH ("+x.cellValue+", "+y.cellValue+")"));
                    }
                }
            }

Upvotes: 0

Views: 185

Answers (1)

Pankaj Kumar Katiyar
Pankaj Kumar Katiyar

Reputation: 1494

     data1 will always be string, One way to check type is:

        //assuming data1 = 12.12
        String data1 = "12.12";

                boolean isInt;
                try{
                    System.out.println(Integer.parseInt(data1));
                    isInt = true;
                }catch(NumberFormatException n1){
                    isInt = false;
                }

                boolean isDouble;
                try{
                    System.out.println(Double.parseDouble(data1));
                    isDouble = true;
                }catch(NumberFormatException n2){
                    isDouble = false;
                }

    Now if you want to check if its a double then use flag -- isDouble, it should be true, or if you want to check if its a number then OR of these two flag should be true: isDouble || isInt ==> true

   //Checking if received data1 is double and valid number then 
    if(!isInt && isDouble)
        {
            double x = Double.parseDouble(data1);
            System.out.println(Math.ceil(x));
            System.out.println(Math.floor(x));
        }
    else if(isInt)
        {
            System.out.println("received int, do nothing");
        }

Upvotes: 1

Related Questions