0

I am trying to make a statement that converts a string to an int according to the following:

very negative = -2
negative = -1 
neutral = 0 
positive= 1 
very positive = 2 

Here is what I have, and for some reason, whenever I call this function it returns 5000, which indicates an error:

  public static int convertSentimentToInt(String sentimentString)
  {
      int sentimentInt;

      if (sentimentString == "Very negative")
        sentimentInt = -2;
      else if(sentimentString == "Negative")
          sentimentInt = -1;
      else if(sentimentString == "Neutral")
          sentimentInt = 0;
      else if(sentimentString == "Positive")
          sentimentInt = 1;
      else if(sentimentString == "Very positive")
        sentimentInt = 2;
      else
          sentimentInt = -5000;

      return sentimentInt;
  }

I know the string I am passing as a parameter is one of these options ("Very negative", "Negative", etc.) because I test it before.

  • Yes the answer in that other question solved my problem, thanks –  Nov 02 '16 at 06:07
  • Bonus points: use an enum (enumerated type—finite list of possible values) instead of a string. `enum Sentiment { VERY_NEGATIVE, NEGATIVE, NEUTRAL, POSITIVE, VERY_POSITIVE; }`. You can combine that with a `switch` statement (or, even better, a static value `Map`) for some pretty elegant conditional logic. – nbrooks Nov 02 '16 at 06:14
  • @nbrooks Thanks I will look into that! –  Nov 02 '16 at 06:43

0 Answers0