Thomas Bartley
Thomas Bartley

Reputation: 23

Can I combine a bunch of if statements into an object?

I have an ArrayList with several thousand single line sentences that I would like to search for roughly 50 key words. If it contains one of these key words, I want it to add a double that corresponds to that key word to an array. Once all the array has been checked for matches, I want to sum up the total of the array.

Currently I have something similar to this except the arraylist and double variable lists are much larger:

import java.util.ArrayList;

public class searchArrayList {

public static void main(String[] args){


String subject2 = "The color of the ball is blue."; 
String subject3 = "The building is white.";
String subject4 = "Red is my favorite color.";  
String subject5 = "Black and blue are the team colors";



    ArrayList<Double> arrlist = new ArrayList<Double>();
    ArrayList<Double> difflist = new ArrayList<Double>();

    double Black = 10.1;
    double Blue = 1.6;
    double Red = 11.4;
    double White = 4.3;

    String[] subjectArray = {subject2,subject3,subject4,subject5};

    for (String subjects: subjectArray)
    {
        if (subjects.toUpperCase().contains("BLUE"));
        System.out.println(Blue+ "Blue ");
        arrlist.add(Blue);
        if (subjects.toUpperCase().contains("BLACK"));
        System.out.println(Black+" Black");
        arrlist.add(Black);
        if (subjects.toUpperCase().contains("WHITE"));
        System.out.println(White+" White");
        arrlist.add(White);
        if (subjects.toUpperCase().contains("RED"));
        System.out.println(Red+" Red");
        arrlist.add(Red);
    }
    System.out.println(sum(arrlist));
}

public static double sum(ArrayList<Double>arrlist)
{
  double result = 0;
  for (double number:arrlist)
  result += number;
  return result;
    }


   }

This does what I need it to, but I was wondering if there is a better or more intuitive way to do this? Could I combine the if statements to a single object / class and then call that class to search the array? Would it be better to use a switch statement? I tried regex for doing the searches but .contains seems to work just as well.

Upvotes: 0

Views: 71

Answers (2)

segfault
segfault

Reputation: 169

I don't have enough reputation to leave a comment, but wouldn't this be more efficient than converting the string to uppercase?

org.apache.commons.lang3.StringUtils.containsIgnoreCase(String, String)

Documentation for the library: http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691675

Just use an array (or a collection) containing your keyword-double associations, and loop over this array:

public class Keyword {
    private String value;
    private double points;

    ...
}

private Keyword[] keywords = new Keyword[] {
    new Keyword("BLUE", 1.6),
    new Keyword("BLACK", 10.1),
    ...
}

for (String subject : subjectArray) {
    for (Keyword keyword : keywords) {
        if (subject.toUpperCase().contains(keyword.getValue())) {
            System.out.println(keyword.getPoints() + " " + keyword.getValue());
            arrlist.add(keyword.getPoints());
        }
    }
}

Upvotes: 3

Related Questions