1

I have 2 arrays which values are words, each word in the first table is associated with a text (String), now each word from second table is showing how many times (int) is repeating in text (String). The expected table should to be like:

Expected Output table

This is the code that I've written so far:

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;
}

I tried to achieve this with Map<String, Integer> dictionary = new HashMap<String, Integer>(); but the results (dictionary.put(searchArray[item], i);) are wrong. Can anyone give me an idea how to solve this. Thanks!

****UPDATE****

Now the results in the console is something like this:

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}

There are values that are repeting. How to make to show just like a table?

2 Answers 2

1

Try using this:

Map<String, Integer> tableMap = new HashMap<String, Integer>();

keep the key as:

tableMap.put("Word1-Search1",23);

Using this, you will always have a unique combination for each key.

I hope you don't want to store the data in a data structure? Instead you should use a 2 dimensional String array to store it.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Danyal, but when I try to put 2 keys "Word1-Search1" is giving an error, is there any way to declare a table with 2 keys. Thanks!!!!
do not put 2 keys main :), concate the string and add it as one string
It's showing the right values in the console, but the same string is repeating, how to do to format like a table without repeating, I updated the answer, please take a look. Thanks
same string cannot repeat..the key is unique in hashmaps
Thank you very much for your help, please tell me if there is a way that the output look like a table that I described above. Thank you!!!
|
0

Answering your latest update:

I think you're getting multiple copies because of this line.

dictionary.put(searchArray[item].concat(string), i);

I think the concat is being applied to the entire row of elements. I would use my debugger to analyze this and see what the value of searchArray[item] is and what the value of string is.

1 Comment

Dear Craig, the expected output is that I have a table where the searchArray[item] is the name of columns and the string is the name of rows and the i is the value of table.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.