Java program that searches items based on the tags

Jessie :

I want to make a program that search an item based on tags. I used a 2D array to put item and its corresponding tag.

            ----------------------------
             Item1 | Tags

               Cat | Pet Cute Mammal
            Burger | Food Snack Mammal
           Chicken | Animal Food Pet
            ----------------------------

So, far my code is working fine but there is one thing I want to improve:

       Example: If I entered "Pet Mammal Cute"
       This is the current output so far:
       Cat
       Cat
       Cat
       Chicken
       My goal is output like this:
       Item | Number of tags matched
       Cat  | (3)
     Chicken| (1)

Below is my code so far:


String [][] data={{"Cat","Pet Mammal Cute"},
                  {"Burger","Food Snack Hot"},
                  {"Chicken","Food Bird Pet"},
                  {"Ice Cream","Food Dessert Cold"},
                  {"Laptop","Gadget Device Computer"},
 };
String getInput= searchInput.getText();
String []in;
int counter=0;
String oldData="";
in = getInput.split(" ");
for (String[] data1 : data) {
     String []container= data1[1].split(" ");

     for(int i=0;i<in.length ;i++){
         for (String container1 : container) {
              if (in[i].equals(container1)) {
                  oldData=oldData +"\n"+ data1[0];

              }
         }
     }
}

result.setText(oldData);
Gaurav Mall :

Your code is fine. It actually does everything great. The problem is where you set the text. Right here:

result.setText(data1[i]);

First of all, if you are using i you will get an array out of bounds exception. So it needs to be like this:

result.setText(data1[0]);

Secondly, you are setting the text, again and again, meaning you are removing the previous text. So you only see the last match. To solve that I would have used a StringBuilder to append the matches together and then after the search set the text. So, your code becomes:

String [][] data={{"Cat","Pet Mammal Cute"},
            {"Burger","Food Snack Hot"},
            {"Chicken","Food Bird Pet"},
            {"Ice Cream","Food Dessert Cold"},
            {"Laptop","Gadget Device Computer"},
};

String getInput= searchInput.getText();;
StringBuilder resultString = new StringBuilder();

for (String[] data1 : data) {
    String [] container = data1[1].split(" ");

    for (String keyword : container)
        if (getInput.equals(keyword)) {
            resultString.append(data1[0]);
            resultString.append(", ");
        }
 }

 result.setText(resultString.toString().substring(0, resultString.length() - 2));

By the way that resultString.length() - 2 at the end is just to remove the last useless commas.

Edit: With your second edit what you are essentially trying to do is associate each item with a property: the number of tags. That's why we will use a HashMap<String, Integer> to do that. This is the code (if you don't understand anything ask me):

String [][] data={{"Cat","Pet Mammal Cute"},
            {"Burger","Food Snack Hot"},
            {"Chicken","Food Bird Pet"},
            {"Ice Cream","Food Dessert Cold"},
            {"Laptop","Gadget Device Computer"},
};

String getInput = searchInput.getText();
String[] in;

in = getInput.split(" ");

HashMap<String, Integer> tags = new HashMap<>();

for (String[] data1 : data) {
     String[] container = data1[1].split(" ");

     for (String s : in) {
          for (String container1 : container) {
               if (s.equals(container1)) {
                   //Here what we do is pass the number of tags. If it hasn't been detected it returns 0. And 0 + 1 = 1. So in the map you store that you have detected the item once and it continues etc.
                   tags.put(data1[0], tags.getOrDefault(data1[0], 0) + 1);
               }
          }
     }
}

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("Items | Number of tags\n");

for (String item : tags.keySet()) {
     stringBuilder.append(item + " | " + tags.get(item) + " Tags Matched\n");
}

//System.out.println(stringBuilder.toString());
//or in your code:
result.setText(stringBuilder.toString());

Basically, we are putting the item with the number of tags. Every time we find the item we increase the number of tags by one with the addition.

This is what it produces when we print:

Items | Number of tags
Cat | 3 Tags Matched
Chicken | 1 Tags Matched

Your desired result :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=340292&siteId=1