我有两个数组,其值是单词,第一个表中的每个单词都与一个文本(字符串)相关联,现在第二个表中的每个单词显示文本(字符串)中重复的次数(int)。预期的表格应该是这样的:
这是我到目前为止编写的代码:
keyW = txtKeyword.getText();
search = textField.getText();
System.out.println("String for car = " + search);
System.out.println("String keyword = " + keyW);
btnUpload.setEnabled(false);
btnNewButton_1.setEnabled(false);
btnNewButton.setEnabled(false);
txtKeyword.setEnabled(false);
textField.setEditable(false);
//waitLabel.setVisible(true);
int iar = 0;
int item;
Map<String, Integer> dictionary = new HashMap<String, Integer>();
String[] searchArray = search.split(",");
String[] itemsFromArray1 = new String[searchArray.length];
//Keyword1 = ("Searched Key"+ "\r\n\t ");
//listKeys.add(Keyword1);
for (iar = 0; iar < searchArray.length; iar++) {
itemsFromArray1[iar] = searchArray[iar].trim();
Keyword1 = (searchArray[iar]);
//listKeys.add(Keyword1);
}
String[] items = keyW.split(",");
for (item = 0; item < searchArray.length; item++) {
WebDriver driver = new HtmlUnitDriver();
((HtmlUnitDriver) driver).setJavascriptEnabled(true);
driver.get("https://en.wikipedia.org/wiki/" + searchArray[item]);
tstr1 = driver.findElement(By.xpath("//*[@id='content']")).getText();
driver.quit();
String[] itemsFromArray = new String[items.length];
for (int i = 0; i < items.length; i++) {
itemsFromArray[i] = items[i].trim();
}
for (String string : itemsFromArray) {
int i = countWords(tstr1, string);
dictionary.put(searchArray[item].concat(string), i);
System.out.println("ARRAY " + dictionary);
}
}
private static int countWords(String tstr1, String string) {
tstr1 = tstr1.toLowerCase();
string = string.toLowerCase();
int posCount = 0;
String positive = string;
Pattern positivePattern = Pattern.compile(positive);
Matcher matcher = positivePattern.matcher(tstr1);
while (matcher.find()) {
posCount++;
}
return posCount;
}
我试图用 Map<String, Integer> dictionary = new HashMap<String, Integer>();
来实现这一点但结果( dictionary.put(searchArray[item], i);
)是错误的。谁能给我一个想法如何解决这个问题。谢谢!
****更新****
现在控制台中的结果是这样的:
ARRAY { boyanimal=4, catfree=18, catanimal=60, boyfree=2, catgender=0, boygender=6, windowfree=5}
ARRAY { boyanimal=4, catfree=18, catanimal=60, boyfree=2, windowanimal=4, catgender=0, boygender=6, windowfree=5}
ARRAY { boyanimal=4, catfree=18, catanimal=60, boyfree=2, windowanimal=4, catgender=0, boygender=6, windowgender=0, windowfree=5}
有些值是重复的。如何让其像表格一样显示?
最佳答案
尝试使用这个:
Map<String, Integer> tableMap = new HashMap<String, Integer>();
将 key 保留为:
tableMap.put("Word1-Search1",23);
使用此功能,您将始终拥有每个键的唯一组合。
我希望您不想将数据存储在数据结构中?相反,您应该使用二维 String
数组来存储它。
关于java - 在for循环java中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32601150/