Displaying duplicate results in arraylist java - java

I am using JDBC to run a SQL query in Java. I want to take the result of the query and store it in an arraylist so that I can display the data in a graph of some sort. I'm getting the same line printing out the same number of times as columnCount. Here is my code.
ArrayList <String[]> result = new ArrayList<String[]>();
int columnCount = rset.getMetaData().getColumnCount();
if(rset!=null)
{
while(rset.next())
{
found=true;
String[] row = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
row[i] = rset.getString(i + 1);
row[i] = rset.getString("Date") + " "
+ rset.getString("Hour");
System.out.println(row[i]);
}
result.add(row);
}

Your second row[i] rewrites the value of the column. Just remove it and you'll see your records:
...
for (int i = 0; i < columnCount; i++) {
row[i] = rset.getString(i + 1);
}
System.out.println(Arrays.toString(row));
result.add(row);
...

Related

How to get the whole row of a JTable to save it into another table?

I am trying to validate that when the column step is equal to 2 the row is copied to another JTable.
But the jtable_step2 I have not initialized correctly,
that's why it returns the error:
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
How do I copy the same columns and rows that satisfy the condition?
Java code:
import javax.swing.JTable;
public class TestJTableCopy {
public static void main(String args[]){
String data[][]={ {"1","/LOCAL/USER/LOCAL", "20220421", "1"},
{"1","/LOACL/USER/LOCAL", "20220421", "2"},
{"1","/LOACL/USER/LOCAL", "20220422", "2"} };
String columns[] = {"LINE", "SOURCE", "DATE", "STEP"};
final JTable jtable = new JTable(data,columns);
JTable jtable_step2 = new JTable();
//jtable2.addColumn(columns);
int row = 0;
for (int i = 0; i < jtable.getRowCount(); i++) {
//STEP == 2
if (jtable.getValueAt(i, 3).equals("2")) {
for(int j = 0; j < jtable.getColumnCount(); j++) {
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
}
row++;
}
}
for (int i = 0; i < jtable_step2.getRowCount(); i++) {
for (int j = 0; j < jtable_step2.getColumnCount(); j++) {
System.out.println(i + " " + j + " " + jtable_step2.getValueAt(i, j));
}
}
}
}
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
You can't use the setValueAt(...) method because the row doesn't exist in the second table. You need to add a new row of data to the model of the table.
You do this by:
Creating a Vector, lets say "row"
Iterate through all the columns of the row you want to copy and add each item to the table
in the second table you use jtable_step2.addRow( row ) to add a new row of data to the model of the table.

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;
}
}
}
}

Displaying the string values of 2 dimensional Object array

I am trying to display the contents of an array after iterating through rows and columns of a JTable. I tried Arrays.toString(myTwoDimensionalArrayVariable) but it won't display the string values.
My goal is to check duplicates for every column per row of a destination JTable when user tries to add row values from a source JTable that's why I want to display the contents of the array.
The values on columns are combination of double, String, and int.
int myRowCount = aJTableParameter.getRowCount();
int myColumnCount = aJTableParameter.getColumnCount();
Object[][] myRowValues = new Object[myRowCount][myColumnCount];
for (int j = 0; j < myRowCount; j++) {
for(int i = 0; i< myColumnCount; i++){
myRowValues[j][i] = aDestinationTable.getValueAt(j, i);
}
}
System.out.println(Arrays.toString(myRowValues));
if (Arrays.asList(myRowValues).contains(column1Value)
&& Arrays.asList(myRowValues).contains(column2Value)
&& Arrays.asList(myRowValues).contains(column3Value)
&& Arrays.asList(myRowValues).contains(column4Value)) {
JOptionPane.showMessageDialog(null, "Duplicate, try again.");
}else{
//do something else
}
I only get this output:
run:
Successfully recorded login timestamp
[]
[[Ljava.lang.Object;#35fa3ff2]
[[Ljava.lang.Object;#407c448d, [Ljava.lang.Object;#1e78a60e]
Is there any other alternative than using 2 Dimensional Arrays?
I'd appreciate any help.
Thanks.
IFF your JTable cells contain only Strings, you can define your array as String[][] instead of Object[][] and fill it with your JTable contents using aDestinationTable.getValueAt(j, i).toString().
EDIT: since that's not the case (as per your comment), it's probably better to use a List, like this:
List<List<Object>> objectList = new ArrayList<>();
for (int j = 0; j < 2; j++) {
objectList.add(j, new ArrayList<>());
for (int i = 0; i < 2; i++) {
if (i==0) objectList.get(j).add("string" + j + i);
if (i==1) objectList.get(j).add((double) 37.8346 * j * i);
}
}
System.out.println("OBJECT LIST: "+objectList);
Output:
OBJECT LIST: [[string00, 0.0], [string10, 37.8346]]
Your code should look like this, then:
List<List<Object>> myRowValues = new ArrayList<>();
for (int j = 0; j < myRowCount; j++) {
myRowValues.add(j, new ArrayList<>());
for (int i = 0; i < myColumnCount; i++) {
myRowValues.get(j).add(aDestinationTable.getValueAt(j, i));
}
}
System.out.println(myRowValues);

Fill array list with object of String[] with SQL table

I'm currently trying to fill an array list with an object of String[] with a SQL table. I was able to get the headers of my tables using this code.
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
int i = 1;
while (colCount >= i) {
String header = rsmd.getColumnName(i);
field.add(header);
i++;
However, when I'm trying to create my rows to fill the table, the array is not adding to the array list.
while (rs.next()) {
String[] row = new String[colCount];
for (int j = 0; j <= colCount; j++) {
row[j] = rs.getString(j+1);
}
data.add(row);
}
} catch (SQLException ex) {
}
System.out.println(data.size());
For some reason when I put the "data.add(row);" statement in the for loop, it only shows the same row however many times it loops. But it doesn't work outside of the for (I want to put it after the for loop inside the while). I'm outputting the size of the list to the console and it just returns 0.
String[] row = new String[colCount];
for (int j = 0; j <= colCount; j++) {
row[j] = rs.getString(j+1);
}
data.add(row);
Dear my friends, why did you decided to write "j <= colCount" meanwhile you had created "j = 0". So your code is going to throw "SqlException". Because index of last element of result set is "colCount", but your loop passed index equals to j + 1. it means equal "colCount + 1".
As you know, you try catch SQLException but you don't do anything. So your code has been failed when first row of resultset and j = colCount + 1. It can't add anything.

Put two arrays to Jtable rows

I have two arrays, which is equal in terms of number of elements. I want to put it in a JTable rows (like in the ascii table example from bellow). I'm using table model and a loop for both arrays, but I archive something else (see print screen).
Note: I want to maintain the correspondence between elements of both arrays, like in the ascii table example.
Integer[] intArray = new Integer[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
intArray[i] = Integer.parseInt(stringArray[i]);
}
System.out.println(Arrays.toString(intArray)); //output [285, 715, 1437, 1749]
Integer[] intArray1 = new Integer[stringArray1.length];
for (int i = 0; i < stringArray1.length; i++) {
intArray1[i] = Integer.parseInt(stringArray1[i]);
}
System.out.println(Arrays.toString(intArray1)); //output [0, 0, 1087, 0]
DefaultTableModel modelPeaks = new DefaultTableModel();
JTable table = new JTable(modelPeaks);
modelPeaks.addColumn("1st Column");
modelPeaks.addColumn("2nd Column");
for (int i = 0; i < intArray.length; i++) {
for (int j = 0; j < intArray1.length; j++) {
modelPeaks.addRow(new Object[]{intArray[i], intArray1[j]});
}
}
The output is:
But I want to archive this:
+--------------------+--------------------+
+ 1st Column + 2nd Column +
+--------------------+--------------------+
+ 285 + 0 +
+ 715 + 0 +
+ 1437 + 1087 +
+ 1749 + 0 +
+--------------------+--------------------+
I think that is from the loop, but I can't figure out how to fix it. Someone can help me? And thanks in advance for your time.
The last loop should read
for (int i = 0; i < intArray.length; i++) {
modelPeaks.addRow(new Object[]{intArray[i], intArray1[i]});
}
Always make sure that both array have the same length.
If the arrays are both the same length you can use the same iterator for both:
for (int i = 0; i < intArray.length; i++) {
modelPeaks.addRow(new Object[]{intArray[i], intArray1[i]});
}

Categories