无法在字符串数组

时间:2016-04-14 21:43:10

标签: java arrays

对于代码,它唤醒用户输入并通过witespace分割它然后从用户输入中获取单个单词并检查单个单词是否在文本文件中(包含并行数组,其中一个是字符串数组,另一个是int数组)。每次它找到用户输入的单词时,它需要添加一个,但问题是我不知道如何实现匹配,或者比较或者equalsTo来检查单词是否在String数组中。

public class MovieReviewSentimentAnalysis {

    static Scanner userInput = new Scanner(System.in);

   public static void main(String[] args) {
      // TODO: complete me

       //make own arrays to pass by value
       //movieReviewComments = the text
       String[] movieReviewComments = new String[10000];
       //movieReviewScores = numeric values, avoid lit. values 
       int[] movieReviewScores = new int[10000];

       String userComment = "";
//     String reviewFile = "";
//     reviewFile = args[0];
       String whiteComment = "";

       MovieReviewReader.readMovieReviews("movie_reviews.txt", movieReviewComments, movieReviewScores); //string, string array, and int array

       System.out.println("Please type one line of review and when you are done press either Ctr D or Ctr Z");
       userComment = userInput.nextLine();
       System.out.println(userComment);

String[] words2  = userComment.split("[\\W]");
       double itemCount = 0;
       double wordTotal = 0;
       double totalSumOfUserCommentWords = 0;
       String test = "";
//     int itemCount = words.length;
       for (int i = 0; i < words2.length; i++)
       {
           test = words2[i];
           itemCount = wordCount(test, movieReviewComments, movieReviewScores);
           wordTotal += itemCount;
           totalSumOfUserCommentWords = wordTotal / userComment.length(); 
//         System.out.println(totalSumOfUserCommentWords);

       }

//     System.out.println(reviewFile);
       System.out.println("Incomplete assignment");

       userInput.close();
   }

   public static double wordCount(String test, String[] movieReviewComments, int[] movieReviewScores)
   {
       double storeScore = 0;
       double totalSumofReviewScores = 0;
       double numOfTimesWordAppears = 0;
       for (int i=0; i < (movieReviewComments.length); i++)
       {
           if (test.equals(movieReviewComments[i])) //////////////////////////////////////////////////////////SOMETHING'S OFF
           {
               storeScore = movieReviewScores[i];
               totalSumofReviewScores += storeScore;

               numOfTimesWordAppears++;
               System.out.println("Found"); //QUQ when will you appear!?!?
           }
           else
              System.out.println("You dun goofed"); //delete after fixing problem
       }
       double wordScoreAverage = totalSumofReviewScores / numOfTimesWordAppears;

       return wordScoreAverage;
   }

1 个答案:

答案 0 :(得分:0)

很简单。您可以通过以下方式执行此操作:

if (movieReviewComments[i].toLowerCase().contains(test.toLowerCase())

如果你想测试一个相等的比较而不是包含,请使用以下代码:

if (test.equalsIgnoreCase(movieReviewComments[i])