2017-05-28 109 views
0

我有这样黄瓜爪哇 - 通过列表名单迭代在Java中

Scenario: Login Page with Valid credentials 
    Given user is on Application landing page 
    Then we verify following user exists 
     | name | email   | phone | 
     | Shankar | [email protected] | 999 | 
     | Ram  | [email protected] | 888 | 
     | Sham | [email protected] | 666 | 

我之情况的文件在我的步骤定义,我想用forforeach循环迭代。我试过用while循环。它工作正常。任何人都可以告诉我如何迭代使用for and foreach loop

@Then("^we verify following user exists$") 
public void we_verify_following_user_exists(DataTable datatable) 
     throws Throwable { 
    List<List<String>> data = datatable.raw(); 

    Iterator<List<String>> it = data.iterator(); 

    while (it.hasNext()) { 
     System.out.println(it.next()); 
    } 

而且我期待的输出像下面

name,email,phone 
Shankar,[email protected],999 
Ram,[email protected],888 
Sham,[email protected],666 

回答

0

你可以用迭代的类似循环:

for (List<String>currentData : data) { 
    org.apache.commons.lang3.StringUtils.join(currentData, ",") 
    System.out.println(System.getProperty("line.separator")); 
} 
+0

它增加了电话后列逗号,这是不是我的预期outpput – Aishu

+0

如果这是一个问题请参阅https://stackoverflow.com/questions/285523/last-iteration-of-enhanced-for-loop-in-java – user7294900

+0

更新解决方案。接受我的解决方案? – user7294900