Width of a column in SWT Table - java

I have a table named tblMainData with several columns with a couple of them with width of 0.
I want to store the all data in the table except those columns with 0 width to a two dimensional array.
How do I check to see if a particular column has 0 width?

Just call TableColumn#getWidth():
for(int col = 0; col < tblMainData.getColumnCount(); col++)
{
if(tblMainData.getColumn(col).getWidth() == 0)
{
// do something
}
}

Related

How to read table row height for ppt table using apache poi?

I have table in one of my ppt slide, my requirement is to read the height of table's row, so if the row is going out of that particular slide i can remove it, the height will vary base on the text in it.
I tried reading like these but for some reason not getting the accurate output:
int totalRows = table.getNumberOfRows();
double rowHeight = 0;
for(int t =0; t< totalTableRows; t++)
{
//logic to read height of row
rowHeight += table.getRows().get(t).getCells().get(0).getAnchor().getHeight();
//logic to remove row.
if(rowHeight > slideHeight)
{
for(int remove = t;t< totalRows; t++)
{
table.removeRow(remove);
}
}
}
Note: some rows and column has merged cells as well.

If cell in specific row contains "x", how to do I retrieve the specific column name where x is present using Java

Sample excel sheet (I am using a much larger excel sheet):
Column0
Column1
Column2
Column3
Column4
Column5
row0
red
x
x
x
row1
orange
x
x
row2
yellow
x
x
x
row3
green
x
x
x
row4
blue
x
x
row5
purple
x
x
row6
pink
x
Let's propose I want to select row3. How do I retrieve the name of the columns where the x' are present? For row3, I want to display on my console "column2 column3 column5". I am also trying to start at a specific column index hence I had a column that strayed from having x's. So for the example, I need to iterate from column2-->column5, then identify in row3 which of the cells have x's and then identify the name of those columns. My code below I had real time numbers I am using.
//find x's in certain row
//pick row 15
int rowIndex = 15;
//start at column 9 and iterate to column 33
for (int columnIndex = 9; columnIndex<33; columnIndex++){
Row row = CellUtil.getRow(rowIndex, sheet);
Cell cell = CellUtil.getCell(row, columnIndex);
//if row contains x in specific cells, get column name
//progTypes is the name of the columns appearing in row index 0
if(cell.getStringCellValue() == "x") {
for(int progTypes = 9; progTypes < 33; progTypes++) {
for (int firstRow = 0; firstRow < 0; firstRow++) {
Row fRow = CellUtil.getRow(firstRow, sheet);
Cell fColumn = CellUtil.getCell(fRow, progTypes);
String columnName = fColumn.getStringCellValue();
System.out.println(columnName);
}
}
}
If I understood correctly you don't need the two for loops inside your if case since you already got the cell index.
if(cell.getStringCellValue() == "x") {
Row fRow = CellUtil.getRow(0, sheet);
Cell fColumn = CellUtil.getCell(fRow, columnIndex);
String columnName = fColumn.getStringCellValue();
System.out.println(columnName);
}
Make use of Apache POI. There are a number of examples how to access and modify spreadsheets at https://poi.apache.org/components/spreadsheet/examples.html

how to get the number of rows used in a particular column

I m using getLastRowNum() and getPhysicalNumberOfCells() for the number of used rows and columns respectively but its not giving the correct index of the row.
int lastRowNum = sheetAt.getLastRowNum();
int lastColNum = sheetAt.getRow(0).getPhysicalNumberOfCells();
Any other option to find out the same???
Rows and Cells can be missing, and there is no built in function to return the number of cells used in a column. So you have to write the function yourself.
int count = 0;
for (Row row : sheet) {
if (row.getCell(5) != null) {
count += 1;
}
}
This retrieves the number of used cells in column F.

java 2d array comparing UPDATED

(UPDATED)
I'm having trouble figuring out what to do here, i need to compare a 2d array that to see if any of the numbers match. I need four numbers to match either up/down, left/right, or diagonal. I just can't get it to test for the down/left diagonal ( / ) here is my updated code
public static boolean isAdjacentFour( int[][] a ) {
// Code to test if column has four adjacent numbers
for ( int row = 0; row <= a.length - 3 ; row++) {
for (int col = 0; col <= a[0].length - 1 ; col++) {
if (a[row][col] == a[row+1][col] && a[row+2][col] == a[row][col]
&& a[row+3][col] == a[row][col]) {
return true;
}
}
}
// Code to test if row has four adjacent numbers
for ( int row = 0; row <= a.length - 1 ; row++) {
for (int col = 0; col <= a[0].length - 3 ; col++) {
if (a[row][col] == a[row][col+1] && a[row][col] == a[row][col+2]
&& a[row][col] == a[row][col+3]) {
return true;
}
}
}
// Code to test if there are 4 adjacent numbers in a down/right ( \ )diagonal
for ( int row = 0; row <= a.length - 3 ; row++) {
for (int col = 0; col <= a[0].length - 3 ; col++) {
if (a[row][col] == a[row+1][col+1] && a[row][col] == a[row+2][col+2]
&& a[row][col] == a[row+3][col+3] ) {
return true;
}
}
}
for ( int row = 0; row <= a.length - 3 ; row++) {
for (int col = 0; col <= a[0].length + 3 ; col--) {
if (a[row][col] == a[row+1][col-1] && a[row][col] == a[row+2][col-2]
&& a[row][col] == a[row+3][col-3] ) {
return true;
}
}
}
return false;
}
Well one problem I noticed in the first nested for loop is this line of code,
for ( int row = 0; row <= a.length - 3 ; row++)
The way you have it now, the row variable is going to be incremented with each iteration of the outer for loop. However, with how you're executing your program, the condition should be row <= a.length - 4 and here's why. Suppose you had a four by four 2D array. The nested for loop will go through one normal iteration of the inner loop, which actually checks every single column for possible matches of four consecutive matching numbers in a column. Here's what it looks like when actually running the program starting at the outer for loop and row = 0,
Iteration One of the inner for loop:
if (a[0][0] == a[1][0] && a[2][0] == a[0][0] && a[3][0] == a[0][0])
Iteration Two of the inner for loop:
if (a[0][1] == a[1][1] && a[2][1] == a[0][1] && a[3][1] == a[0][1])
Iteration Three of the inner for loop:
if (a[0][2] == a[1][2] && a[2][2] == a[0][2] && a[3][2] == a[0][2])
Last Iteration of the inner for loop:
if (a[0][0] == a[1][3] && a[2][3] == a[0][3] && a[3][0] == a[0][3])
Once this is done, the row variable will be incremented according to the outer loop definition. This is what is most likely causing an error because now as we start iterating through the inner loop with row = 1, this happens
Iteration One of the inner for loop:
if (a[1][0] == a[2][0] && a[3][0] == a[1][0] && a[4][0] == a[1][0])
Here, we already have an indexoutofboundsexception when trying to access the 5th row in a 4x4 2D array. So the simple fix here is to change
for(int row = 0; row <= a.length - 3 ; row++)
to
for(int row = 0; row <= a.length - 4 ; row++)`
A similar argument can be made for the second nested for loop. If you don't believe me, do something similar to what I did for the rows and write out the iterations for it using a 4x4 2D array. You'll get the arrayindexoutofbounds exception in the first iteration of the outer for loop and in the second iteration of the inner for loop when row = 0 and col = 1, causing the program to make a check at the fifth column in the first row of a 2D array. So the simple fix should be to change
for (int col = 0; col <= a[0].length - 3 ; col++)
to
for (int col = 0; col <= a[row].length - 4 ; col++)
Personally, I prefer to use a[row].length only because there could be some instances where it's not a perfect nxn 2D array. For example, some rows may have only 3 columns where the first row has 7 columns. If this is the case, then you'll get an outofbounds exception for trying to access columns that exist in the first row that don't exist in other rows.
For the third nested for loop, again, same argument can be made just by writing out the iterations and it should be changed to
for(int row = 0; row <= a.length - 4 ; row++) {
for(int col = 0; col <= a[row].length - 4 ; col++)
The last nested for loop has a logic problem pertaining to the inner for loop. Since you're decrementing from 0, you're going to get an out of bounds exception just from trying to access negative indices in the array. So the simple fix should be to initialize col to the last column and change the condition to col being greater than or equal to 3 since you're accessing elements at the columns, col, col-1, col-2, and col-3. If that is confusing, think about it this way. You're checking columns starting from col and the three that come before it. What if there aren't even four columns to begin with? This is why there's the condition col >= 3 because you check a column and the three that come before it until you've reached column #4 (col = 3). Once you've reached column #3 (col = 2), there's no way of checking that column and the three before because there are only 3 columns to check at this point. Changes similar to the other 3 nested loops should be made to for ( int row = 0; row <= a.length - 3 ; row++) regarding the -3. It should end up looking like this,
for ( int row = 0; row <= a.length - 4 ; row++) {
for (int col = a[row].length - 1; col >= 3; col--)
As a rule of thumb , always put debug points to see why you are getting exception/error.
The issue here is your outer loop runs from 0 to row-1 . But inside in inner loop you are using [row+2] and [row+3] and [row+1] . Now when the outer loop goes to row-2 iteration , you will get a out of bound exception.
Can post the code here , but if you understand this , you should be able to solve it.
(EDIT) : example as asked in comment.
assume you have a 2D array A[][]of size 10X10 .
Now if the current loop is at A[4][4] or (A[row][col]) :
left element : A[4][3] or (A[row][col-1]) // here we are at same row but (column -1) as we want the left element.
top-right element : A[3][5] or ((A[row-1][col+1]) // here we are going to (4-1) row as we are interested in above row and (4+1)column as we want right element.
bottom-left: A[5][3] (A[row+1][col-1]) ...
Now the two consecutive bottom-left elements will be (A[row+1][col-1]) and (A[row+2][col-2]).
Try visualizing it by drawing a 2D array and naming each cell in terms of A[i][j].

analyzing a 2D array by columns

I have the following 2D array and I want to compare all the columns with each other.
int [][] myarray={{1,2,3},{1,2,3},{1,2,3}};
So what I want to see is if column 1 (all 1's) is equal to the values in column 2 (all 2's).
Ps. the array size is not just limited to this.
It's not quite clear from your question whether you want to compare all columns to each other, or just a single column to another single column (for example column 1 to column 2). Assuming you meant the latter, you could do this.
public boolean columnsIdentical(int[][] array, int colIndex1, int colIndex2) {
for (int row = 0; row < array.length; row++ ) {
if (array[row][colIndex1] != array[row][colIndex2]) {
return false;
}
}
return true;
}
for (int i=0;i<myarray[0].length;i++) {
int comp=myarray[0][i];
for (int j=1;j<myarray.length;j++) {
if (myarray[j][i] != comp) {
// no match
} else {
// match
}
}
}
To test all pairs of columns you need 3 loops
Innermost compares elements of columns A and B
Middle loops through B, skipping columns already checked
Outermost loops through A for all columns

Categories

Resources