• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

query on for loop

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
input

raj pass
raj Fail
raj pass
abc pass
abc pass
abc pass

output

raj Fail
abc pass

can anyone help me out to do this operation using for loop or do while loops. I am taking these data from excel sheet.



 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RajKumar Kanakaraj wrote:can anyone help me out to do this operation using for loop or do while loops.


No. Or at least not until you come up with a better specification.
I could guess at why the output is like it is, but I might be wrong and so would be wasting my time telling you how to produce it.
 
RajKumar Kanakaraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, using a for loop am reading these data.

File excel = new File ("C:/test.xlsx");

FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheetAt(0);

int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
String [][] data = new String [rowNum] [colNum];

for(int i = 1; i <rowNum; i++)
{
XSSFRow row = ws.getRow(i);

System.out.println ("the first value " + row.getCell(1).toString());
System.out.println ("the second value " + row.getCell(2).toString());

}

This gives me output as;

the first value A
the second value PASS
the first value A
the second value PASS
the first value A
the second value FAIL
the first value B
the second value PASS
the first value B
the second value PASS
the first value B
the second value PASS

MATRIX LOOKS LIKE

A PASS
A PASS
A FAIL
B PASS
B PASS
B PASS

I want to add more loops to filter the data as, data containing A has any thing FAIL status then output of that will be A FAIL

again if next set of data containing the same name should check the status if all are in PASS condition then resultant should be B PASS.

the over all output should look like

A FAIL
B PASS

using this in loop;

System.out.println ("the first value " + row.getCell(1).toString());
System.out.println ("the second value " + row.getCell(2).toString());


Its just filtering data from the given data.

Thanks


 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a Map that uses the first value as the key and the second value as the value.
If the second value is FAIL you can just add it to the map without worrying about the current value.
If the second value is PASS you will need to add it only if the current value isn't FAIL.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:Use a Map that uses the first value as the key and the second value as the value.
If the second value is FAIL you can just add it to the map without worrying about the current value.
If the second value is PASS you will need to add it only if the current value isn't FAIL.



Better yet, if the second value is PASS then it only needs to be added if the key is not present. With the ConcurrentHashMap, there is even a method for this -- the putIfAbsent() method.

Henry
 
RajKumar Kanakaraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Stuart A. Burkett wrote:Use a Map that uses the first value as the key and the second value as the value.
If the second value is FAIL you can just add it to the map without worrying about the current value.
If the second value is PASS you will need to add it only if the current value isn't FAIL.



Better yet, if the second value is PASS then it only needs to be added if the key is not present. With the ConcurrentHashMap, there is even a method for this -- the putIfAbsent() method.

Henry




Thanks Henry
 
RajKumar Kanakaraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:Use a Map that uses the first value as the key and the second value as the value.
If the second value is FAIL you can just add it to the map without worrying about the current value.
If the second value is PASS you will need to add it only if the current value isn't FAIL.



Thanks Stuart
 
reply
    Bookmark Topic Watch Topic
  • New Topic