2017-05-28 67 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