java - How to break the if condition? -
package com.selenium.utitlity; import java.io.file; import java.io.ioexception; import java.util.hashtable; import org.apache.poi.xssf.usermodel.xssfrow; import org.apache.poi.xssf.usermodel.xssfsheet; import org.apache.poi.xssf.usermodel.xssfworkbook; public class readrows { public static hashtable<string, string> getrowvalues(int row) throws ioexception { file file = new file("./prop.xlsx"); if (file.exists()) { hashtable<string, string> table = new hashtable<string, string>(); xssfworkbook workbook = new xssfworkbook("./prop.xlsx"); xssfsheet sourcesheet = workbook.getsheet("sheet1"); int lastrow = sourcesheet.getlastrownum(); // getheading of row xssfrow headerrow = sourcesheet.getrow(0); // test data xssfrow sourcerow = sourcesheet.getrow(row); if (row > lastrow) { system.out.println("the row out of range"); } else { int lastcolumn = headerrow.getlastcellnum(); (int = 0; < lastcolumn; i++) { table.put(headerrow.getcell(i).tostring(), sourcerow .getcell(i).tostring()); } } return table; } else { system.out.println("no such file there in directory"); return null; } } }
i trying use break or exit in following piece of code:
if (row > lastrow) { system.out.println("the row out of range"); }
to exit code not return table null. if break not used getting following result
the row out of range null
i dont want print null wanted use break when row > lastrow
first i'd point out if
statements not loops. may used recursively that's out of natural use. in order break or continue loops may simple use break
or continue
keyword.
these used both in labelled , unlabelled contexts. check out oracle docs more in-depth explanation.
break
used terminate for
, while
& do-while
not usable in context. should throw
exception instead. can catch exception , whatever wish it.
you may use both break
, continue
in if
statement when it's inside loop.
hope helps.
Comments
Post a Comment