How to make a counter and reset if condition doesn't match - java

I'm writing a code where I have to compare data between excel rows, if I find a match I want to increment my finalIndex by 1 and once the matches are found I need to merge the cells. Here I'm unable to understand how to maintain the track of the starting row number.
Below is my code. How can I do this?
HSSFWorkbook workbook = new HSSFWorkbook(fin);
HSSFSheet sheet = workbook.getSheetAt(0);
int row = sheet.getPhysicalNumberOfRows();
String currentLawName, currentCountry, currentAssociate, previousLawName, previousCountry, previousAssociate;
String currentPages, previousPages;
int startIndex = 0, finalIndex = 0, tempNum = 0;
System.out.println(row);
for (int i = 2; i < row; i++) {
currentAssociate = sheet.getRow(i).getCell(1).toString();
currentLawName = sheet.getRow(i).getCell(3).toString();
currentCountry = sheet.getRow(i).getCell(4).toString();
currentPages = sheet.getRow(i).getCell(5).toString();
previousAssociate = sheet.getRow(i - 1).getCell(1).toString();
previousLawName = sheet.getRow(i - 1).getCell(3).toString();
previousCountry = sheet.getRow(i - 1).getCell(4).toString();
previousPages = sheet.getRow(i - 1).getCell(5).toString();
if (currentAssociate.equals(previousAssociate) && currentCountry.equals(previousCountry)
&& currentLawName.equals(previousLawName) && currentPages.equals(previousPages)) {
finalIndex += 1;
} else {
finalIndex = 0;
}
}
System.out.println(finalIndex + "\t" + tempNum);

Related

Selenium WebDriver: Finding the row of a table containing two input values

I'm working on function that takes two values in a table then verifies the row that contains the two values to verify if a third value exist in that row.
The code below finds the row where the two values are. However, it will not break out of the for loop if rowNo and rowNo2 are equal. I've tried using .equals() but that gives me an error. Any tips?
try {
// Get the row number of passed row Data
WebElement table = oWebDriver.getWebElement(tableObject);
List<WebElement> rows = table.findElements(By.tagName("tr"));
List<WebElement> cols = table.findElements(By.tagName("td"));
int colSize = (cols.size()) / (rows.size());
int rowNo = 0;
int rowNo2 = 0;
int colNo = 0;
for (int i = 0; i < cols.size(); i++) {
WebElement cellData = cols.get(i);
if (cellData.getText().contains(rowData)||cellData.getText().contains(rowData.toUpperCase())) {
int l = i / colSize;
rowNo = l + 1;
for (int k = 0; k < cols.size(); k++) {
WebElement cellData2 = cols.get(k);
if (cellData2.getText().contains(rowData2)||cellData2.getText().contains(rowData2.toUpperCase())) {
int n = k / colSize;
rowNo2 = n + 1;
}
System.out.println("row No2 = " + rowNo2);
System.out.println("row No1 = " + rowNo);
if (rowNo == rowNo2) {
break;
}
}
}
}

How to continue to loop and create new cell based on arraylist

I have a string arraylist name as code like this:
[a,b,c,d]
and another string arraylist that contain data like this name as DWS:
[a11,a22,a33,a44,a55,a66,a77,b11,b22,b33,b44,b55,b66,b77,c11,c22,c33,c44,c55,c66,c77,d11,d22,d33,d44,d55,d66,d77]
What I want to do is I want to write to the DWS arraylist into excel file like this:
I have tried this way:
FileInputStream file1 = new FileInputStream(new File(namefile));
XSSFWorkbook wb1 = new XSSFWorkbook(file1);
XSSFSheet sheet1 = wb1.getSheetAt(0);
int limit = 7;
for (int m = 0; m < code.size(); m++) {
for (int x = 0; x < DWS.size(); x++) {
for (int i = 0; i < limit; i++) {
Row row2 = sheet1.getRow(i + 2);
Cell cell = row2.createCell(m * 3 + 4);
cell.setCellValue(DWS.get(x));
}
}
}
but the result that I get is:
Any idea how to overcome this problem?
UPDATE
I change my code like this as suggested:
int limit = 7;
for (int i = 0; i < limit; i++) {
Row row2 = sheet1.getRow(i + 2);
for (int m = 0; m < code.size(); m++) {
Cell cell = row2.createCell(m * 3 + 4);
cell.setCellValue(DWS.get(i));
}
}
and the result is like this:
It only loop the first 7 data. I need it to continue for the rest of the arraylist. like the first image.
UPDATE
let me explain again the flow of my code
1) It will loop the DWS arraylist into the first cell which is cell no 5 and loop until the last row which row no 7,
2) then , after it reach row no 7, it then supposedly create a new cell which in line Cell cell = row2.createCell(m * 3 + 4);and then again, continue looping from the DWS arraylist data.
3) this steps will continue until it reach the last arraylist data of DWS
is that possible to do that?
UPDATE 2.0
I change to this:
int cellValueIndex = 0;
for (int i = 0; i < limit; i++) {
Row row2 = sheet1.getRow(i + 2);
for (int m = 0; m < code.size(); m++) {
Cell cell = row2.createCell(m * 3 + 4);
cell.setCellValue(DWS.get(cellValueIndex));
cellValueIndex++;
}
}
but I got error like this:
java.lang.IndexOutOfBoundsException
UPDATE 3.0
I change to this but the result is still wrong
int cellValueIndex = 0;
for (int i = 0; i < limit; i++) {
Row row2 = sheet1.getRow(i + 2);
for (int m = 0; m < code.size(); m++) {
Cell cell = row2.createCell(m * 3 + 4);
cell.setCellValue(DWS.get(cellValueIndex));
}
cellValueIndex = cellValueIndex + 1;
}
UPDATE 4.0
I have change to this:
int cellValueIndex = 0;
int counter = 0;
for (int i = 0; i < limit; i++) {
Row row2 = sheet1.getRow(i + 2);
Cell cell = row2.createCell(4);
cell.setCellValue(DWS.get(cellValueIndex));
cellValueIndex++;
int x = row2.getRowNum();
if (x == limit) {
for (int m = 1; m < code.size(); m++) {
Cell cell1 = row2.createCell(m * 3 + 4);
cell1.setCellValue(DWS.get(cellValueIndex + 1));
cellValueIndex++;
}
}
}
The result is like this:
But when I try to set the i=0;, like this:
int cellValueIndex = 0;
for (int i = 0; i < total1; i++) {
Row row2 = sheet1.getRow(i + 2);
Cell cell = row2.createCell(4);
cell.setCellValue(DWS.get(cellValueIndex));
cellValueIndex++;
int x = row2.getRowNum();
if (x == total1) {
i = 0;
for (int m = 1; m < shiftcode.size(); m++) {
Cell cell1 = row2.createCell(m * 3 + 4);
cell1.setCellValue(DWS.get(cellValueIndex));
cellValueIndex++;
i++;
}
}
}
I got error like this:
java.lang.IndexOutOfBoundsException
There are 3 for loops going on here! However as I see, you are creating cells in the third loop, where the value of the x is the last representational value from the DWS. If DWS is just a source of the cell values then you should have a separate index for that rather than keeping a loop for that.
In this case, though you don't realize from the end result, it is creating DWS.size times tables and it's cells! You don't realize it from the result because what you see is the last executed table which contains the value d77 only. And you are putting ONLY one value to all the cells because in the last loop DWS.get(x) is always constant, you don't have to do that.
You have to put different values to each and every cell.
Solution:
You need just 2 loops.
int cellValueIndex = 0;
for (int m = 0; m < limit; m++) {
// create a row here
for (int n = 0; n < code.length; n++) {
// take the value from DWS like DWS.get(cellValueIndex) and
// keep incrementing cellValueIndex by one always
}
}
If you use apache csv printer you can print rows easily. I wrote a solution for you that work:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class DataGenerator {
private static final String[] HEADERS = {"a", "b", "c", "d"};
private static final String[] DATA = {"a11", "a22", "a33", "a44", "a55", "a66", "a77", "b11", "b22", "b33", "b44", "b55", "b66", "b77", "c11", "c22", "c33", "c44", "c55", "c66", "c77", "d11", "d22", "d33", "d44", "d55", "d66", "d77"};
private static CSVPrinter csvPrinter;
private static final CSVFormat getFileCsvFormat = CSVFormat.newFormat(';')
.withHeader(HEADERS)
.withRecordSeparator('\n');
private static final int rowSize = DATA.length / HEADERS.length; // 7
#BeforeAll
private static void setupCsv() throws IOException {
final long date = new Date().getTime();
final String fileTestData = "src/test/resources/csv/testdata.csv";
final FileWriter getDataWriter = new FileWriter(fileTestData);
csvPrinter = new CSVPrinter(getDataWriter, getFileCsvFormat);
System.out.println("Files created:" + fileTestData);
}
#Test
void printCsv() throws IOException {
final int dataLength = DATA.length;
final List<String[]> columns = getColumns(DATA);
final String[][] rows = transformToRows(columns);
for (int i = 0; i < rows.length; i++) {
printRow(rows[i]);
}
csvPrinter.flush();
}
private void printRow(final String[] row) {
try {
csvPrinter.printRecord(row);
} catch (final IOException e) {
e.printStackTrace();
}
}
private List<String[]> getColumns(final String[] data) {
final List<String[]> partitions = new ArrayList<>();
final int dataLength = data.length;
for (int i = 0; i < dataLength; i += rowSize) {
partitions.add(Arrays.copyOfRange(DATA, i, Math.min(i + rowSize, dataLength)));
}
return partitions;
}
/**
* a1 a2
* b1 b2
* - transform to: -
* a1 b1
* a2 b2
*/
private String[][] transformToRows(final List<String[]> columns) {
final String[][] dataMatrix = new String[rowSize][HEADERS.length];
for (int i = 0; i < columns.size(); i++) {
for (int j = 0; j < rowSize; j++) {
//{{a1, a2}, {b1, b2}}
dataMatrix[j][i] = columns.get(i)[j];
}
}
return dataMatrix;
}
I think your main problem however was how to transform your data from a column structure to a row structure, which you can print left to right. The input data is split into columns easily, but most csv/excel/... printers work by row. You can use the transformToRows method from my example to do this.

Align text center in the merged region in excel using apache poi in java

I have created an excel sheet, where I am inserting the value to cell and merging cells vertically at the same time and I am able to achieve that but the problem is that, I am not able to align text vertically center in the merged cell.Currently, text is showing at the bottom of vertically merged cell.
here's my code,
CellStyle cs = null;
cs = wb.createCellStyle();
cs.setWrapText(true);
//cs.setAlignment(CellStyle.ALIGN_CENTER);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFont(f);
Row row[]=new Row[pageListAcrToDept.size()];
for (int i = 0; i < pageListAcrToDept.size(); i++) {
rowIndex = 8 + i ;
row[i]=sheet.createRow(rowIndex);
}
List<List<String>> datas=new ArrayList<>();
datas.add(pageListAcrToDept);
datas.add(notesListAcrToDept);
datas.add(treatmentListAcrToDept);
datas.add(upcListAcrToDept);
datas.add(itemCodeListAcrToDept);
datas.add(descListAcrToDept);
datas.add(subDeptListAcrToDept);
datas.add(limitListAcrToDept);
datas.add(XforListAcrToDept);
datas.add(priceListAcrToDept);
datas.add(couponListAcrToDept);
datas.add(adzoneListAcrToDept);
datas.add(promoDescListAcrToDept);
for (int column = 0; column < 13; column++) {
List <String> list=datas.get(column);
int index=0;
for (int i = 0, prev = -1; i < list.size(); i++) {
if (i == list.size() - 1 || ! list.get(i).equals(list.get(i + 1))) {
//System.out.printf("number: %d, count: %d%n", list.get(i), i - prev);
for(int pos=0;pos<i - prev;pos++){
int posi=index+pos;
Cell cell= row[posi].createCell(column);
cell.setCellStyle((CellStyle) cs);
cell.setCellValue(list.get(i));
}
int startrowpos=index+8;
int endrowpos=index+8+(i - prev)-1;
if(startrowpos==endrowpos){
LOG.info("don't merge");
}else{
CellRangeAddress cellRangeAddress = new CellRangeAddress(startrowpos, endrowpos,
column, column);
sheet.addMergedRegion(cellRangeAddress);
}
index=index+(i - prev);
prev = i;
}
}
}
Finally, I resolved this problem. I am posting here the changes,because it may help other.
for (int column = 0; column < 13; column++) {
List <String> list=datas.get(column);
int index=0;
for (int i = 0, prev = -1; i < list.size(); i++) {
if (i == list.size() - 1 || ! list.get(i).equals(list.get(i + 1))) {
//System.out.printf("number: %d, count: %d%n", list.get(i), i - prev);
int posi=0;
for(int pos=0;pos<i - prev;pos++){
if(pos==0){
posi=index+pos;
}
}
int startrowpos=index+8;
int endrowpos=index+8+(i - prev)-1;
if(startrowpos==endrowpos){
LOG.info("don't merge");
Cell cell= row[posi].createCell(column);
cell.setCellStyle((CellStyle) cs);
cell.setCellValue(list.get(i));
}else{
CellRangeAddress cellRangeAddress = new CellRangeAddress(startrowpos, endrowpos,
column, column);
sheet.addMergedRegion(cellRangeAddress);
Cell cell= row[posi].createCell(column);
cell.setCellStyle((CellStyle) cs);
cell.setCellValue(list.get(i));
}
index=index+(i - prev);
prev = i;
}
}
}

fastExcel:a column missing when reading

fastexcel is the newest.
create a xsl file first,then go to read it,the results showed that the last column lost.However, when I saved the XLS documents again,the last column is not last when reading.
Generated code:
java code
Workbook workBook;
workBook = FastExcel.createWriteableWorkbook(xls);
workBook.open();
int rowsLength = rows.length;
int sheets = rowsLength / sheetRows
+ (rowsLength % sheetRows > 0 ? 1 : 0);
int sheetNumber;
for (int i = 0; i < sheets; i++) {
sheetNumber = i + 1;
Sheet sheet = workBook.addStreamSheet(sheetName + sheetNumber);
// sheet.addRow(titles);
for (int j = i * sheetRows; j < rowsLength; j++) {
sheet.addRow(rows[j]);
if (j >= sheetRows * sheetNumber) {
break;
}
}
}
workBook.close();
code by read
java code
Workbook workBook;
workBook = FastExcel.createReadableWorkbook(xls);
workBook.setSSTType(BIFFSetting.SST_TYPE_DEFAULT);
workBook.open();
Sheet s;
s = workBook.getSheet(0);
excelContent = new String[s.getLastRow()-startRow][];
for (int i = startRow; i < s.getLastRow(); i++) {
excelContent[i-startRow] = s.getRow(i);
}
workBook.close();
u may change
if (j >= sheetRows * sheetNumber) {
break;
}
to
if (j >sheetRows * sheetNumber) {
break;
}

Excel is missing some values while writing

I am new to Apache-poi and using 3.8 version of poi. While writing value in the Excel,i check for the column names if they matched, i will write some value in that column and finish it, and again i will start writing on header. The problem is if write three column values only last column values are add or saved and first two values are not saved in the column. Can anyone tell what went wrong.
(sorry, in case any mistake)
Code:
int i = 0;
Row row = sheet.createRow(i);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_ORANGE.index);
String validate_header = null;
while (iterator.hasNext()) {
if (eiterator.hasNext()) {
validate_header = eiterator.next();
}
Cell cell = row.createCell(i);
String col_heading = iterator.next();
cell.setCellValue(col_heading);
if(col_heading.equalsIgnoreCase("Assigned Date"))
{
Add_value(i, col_heading, row, sheet);
row=sheet.getRow(0);
cell=row.getCell(i);
}
else if(col_heading.startsWith("Review"))
{
int count=-1;
int n=Col_values.get("Defect Summary").size();
for (int j = 0; j < n; j++) {
row = sheet.createRow(count);
cell = row.createCell(i);
String s="External QC Defect ";
cell.setCellValue(s);
count++;
}
row=sheet.getRow(0);
cell=row.getCell(i);
}
sheet.autoSizeColumn(i);
i++;
}
private static Sheet Add_value(int k,String name,Row row, Sheet sheet) {
System.out.println("Inside the add method");
if(name.equalsIgnoreCase("Assigned Date")||name.equalsIgnoreCase("Reported Date") )
{
vector = Col_values.get("TargetDate");
int count = 1;
System.out.println("IF Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
else
{
vector = Col_values.get("Defect Summary");
int count = 1;
System.out.println("ELSE Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
return sheet;
}
'
Can u tell what went Wrong?
It seems that Add_value starts creating rows from top. Therefore on the second call the old rows are removed.
Replace
row = sheet.createRow(count);
with
row = k == 0 ? sheet.createRow(count) : sheet.getRow(count);

Categories