Printing pattern from list in Java
Printing pattern from list in Java
What I have done here is taken the contents of the table below and stored them in a list called tableElems
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 List tableElems = chrome.findElements(By.tagName("td")); From this table I want to print in a pattern starting at the 4th element (#3 in the table) and I want to print 3 elements, then skip the next 6, print the next 3 and then skip the next 6, etc. etc.
So my expected output would be 3 4 5 12 13 14 21 22 23 30 31 32
My initial attempt ended with
for (int i = 3; i Obviously this is wrong, because my % 3 will print the 9th element, % 4 will print the 16th element, etc.
I am having trouble wrapping my head around this, so any advice to point me in the right direction is appreciated!
Answer by Ali Torabi for Printing pattern from list in Java
Update: Java code
for (int i = 3; i If you are creating a larger pattern:
int starting = 3; int nextStartDistance = 9; int waveCount = 3; for (int i = starting; i This code is written in JavaScript, but the algorithm considered:
var list =[ 0,1,2,3,4,5,6,7,8, 9,10,11,12,13,14,15,16,17, 18,19,20,21,22,23,24,25,26, 27,28,29,30,31,32,33,34,35]; for (var index = 3; index <= list.length; index +=9 ) { var a = list[index]; var b = list[index+1]; var c = list[index+2]; var d = list[index+3]; console.log(a,b,c,d); } Result will be what you expected:
// 3 4 5 6 // 12 13 14 15 // 21 22 23 24 // 30 31 32 33 // user:~$ Answer by Jigar Joshi for Printing pattern from list in Java
int offset = 4; int width = 3; advice:
for each row in table check offset + width is under the row width, and then start from offset + 0, offset + 1 upto offset + width
and continue this loop for all the row of table
Answer by Arthur Baczyk for Printing pattern from list in Java
You mentioned that tableElems is a list, so I assume it's akin to a single-dimension array, rather than what appears to be a multi-dimensional table like you've laid out(I assume for visual purposes).
You're right by starting the for loop at i = 3. The easiest way, IMO, to do this is to just bump the loop ahead several spaces in the array after you've printed three elements. Here's my solution using C# and an array; you should be able to convert it to Java pretty easily.
int counter = 0; for (int i = 3; i < array.Length; i++) { if (counter < 3) { Console.WriteLine(array[i]); counter++; } else { i += 5; // we are incrementing by one on the next pass, so this can be 5 counter = 0; } } Answer by hujasonx for Printing pattern from list in Java
try this:
for (int i = 3; i Think about this as dividing into groups of nine, and further dividing each group of 9 into groups of 3. on the second group of 3 (i.e. 3-5, 12-14) it will display.
Answer by Andrey Bardyshev for Printing pattern from list in Java
If you want to stick to %, then this should work
int rowSize = 9; for (int i = 0; i < tableElems.size(); i++) { int column = i % rowSize; if (column >=3 && column <= 5) { System.out.println(tableElems.get(i).getText()); } } Answer by Mincong Huang for Printing pattern from list in Java
for (int i = 3; i < tableElems.size(); i += 9) { for (int j = i; j < i + 3 && j < tableElems.size(); j++) { System.out.println(tableElems.get(j).getText()); } } Answer by Matthias Schmidt for Printing pattern from list in Java
i think this would be very close to your code:
for (int i = 3; i < tableElems.size(); i++) { if (i % 3 == 0) { System.out.println(tableElems.get(i).getText()); } else if ((i % 3) == 1) { System.out.println(tableElems.get(i).getText()); } else if ((i % 3) == 2) { System.out.println(tableElems.get(i).getText()); i = i + 6; } } i just adapted your modulo and skip 6 elements every time i hit the last one to print out
furthermore its quite effective because all unneeded elements are just skipped instead of iterated and checked
Answer by DJ. for Printing pattern from list in Java
Assuming you only care about the values out of your table and the html structure is standard html tags as shown on your questions, then you can use css selector to select appropriate values.
chrome.findElements(By.cssSelector("td:nth-child(4),td:nth-child(5), td:nth-child(6)")) Please note that css nth-child selector is 1 based.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:
Post a Comment