2016-04-14 44 views
0

對於代碼,它會喚醒用戶輸入並將其拆分爲witespaces,然後從用戶輸入中獲取單個單詞並檢查單數單詞是否在文本文件中包含並行數組,其中一個是字符串數組,另一個是int數組)。每次找到用戶輸入的單詞時,都需要添加一個單詞,但問題是我不知道如何實現match,compare或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; 
    } 
+0

的'字符串#equals'方法區分大小寫。也許應用'.toLowerCase()'?向我們展示一些測試數據請 – Norsk

回答

0

這很簡單。你可以做到這一點通過以下方式:

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

如果你想測試一個平等的比較,而不是遏制,使用以下代替:

if (test.equalsIgnoreCase(movieReviewComments[i])

+0

謝謝我的確嘗試了contains方法,它的工作原理我只需要能夠找到字符串數組是否包含像從他們那裏帶走嘿。 – Earl

+0

很酷。如果有效,請不要忘記標記回答。 – VHS

相關問題