How to read a cell value randomly using random function from an excel file in Java?

2.3k views Asked by At

I have an excel sheet having 10 entries , 0-9 rows & 0 columns. I want to read a random value from the cell of (5,0) ex (row, column) using random function ? Below is the code i tried !

FileInputStream fis = new FileInputStream("C://Users//logasaravanan.g//Desktop//seleniumtest.xls");
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheet("xxxx");
Random rand = new Random();
int a= rand(sheet.getLastRowNum());
HSSFRow row = sheet.getRow(a);
System.out.println("Running test case " + row.getCell(0).toString());
1

There are 1 answers

1
Tschallacka On BEST ANSWER

You use nextInt

int row = rand.nextInt(sheet.getLastRowNum());

Your error lies in the fact that you try to use your rand Object as a method.
That's not how it works. Your rand is a Object of the type Random

You then access methods ON that object to achieve your goals. You can kinda see it as a toolbox.

By doing Random rand = new Random() you're getting your toolbox off the shelf. Then you can use tools that are IN the toolbox, like rand.nextInt