2012-04-01 7 views
-1

可能性の重複:
Loop Keyword Program HomeworkJavaのキーワード平均掲載順位

私は、文字列で、ユーザ入力されたキーワードの平均開始位置を見つけようとしています。私はindexOf()を使ってみましたが、正しく初期化できないようです。誰か助けてくれますか?

私が探している出力例は次のようになります。平均的な開始位置はwhileループ内の2つのif sが終わり;を持っている4.5

import java.util.Scanner; 

public class Lab6Loops { 

    public static void main(String[] args) { 

     String keywordString; 
     String inputString; 
     Scanner keyboard = new Scanner (System.in); 
     int numofSentences = 0; 
     int numofKeyword = 0;      
     System.out.println ("Enter a keyword. We will search each sentence for this word."); 
     keywordString = keyboard.nextLine(); 
     System.out.println ("Please enter a sentence or type 'stop' to finish"); 
     inputString = keyboard.nextLine(); 
     while(!inputString.equals ("stop")) 
     {  
      if(inputString.contains (inputString)); 
      numofSentences = numofSentences + 1; 
      if(inputString.contains (keywordString)); 
      numofKeyword = numofKeyword + 1; 
      System.out.println ("Enter a line of text or 'stop' to finish"); 
      inputString = keyboard.nextLine(); 
     } 
     System.out.println ("You entered " + numofSentences + " sentences"); 
     System.out.println ("You have " + numofKeyword + "sentences that contain the keyword"); 
    } 
} 
+2

あなたが実際に取得する出力は何ですか?デバッガであなたのプログラムを踏んだときに何を学びましたか? –

+0

私はしました averagePosition = indexOf(keywordString); しかし、それは動作しませんでした。 numofSentencesとnumofKeywordで行ったように変数を初期化する必要がありますか? – user1276514

+0

私は文字列内の単語をindexOfにする方法を知っていますが、問題は私がユーザー入力キーワードを行う方法を知らないということです。 私は例えばindexOf( "word")を使うことができます。 – user1276514

答えて

0

です。それらを削除して、プログラムを再実行してみてください。

+0

または、if節のまわりに常に中括弧を使用してください。 – beerbajay

0

スキャナクラスでは、パターン内でパターンが発生した場所を知る方法はありません。あなたはパターンとMatcherクラスを使って位置を見つけなければなりません。これはあなたの宿題イムあるとしてこのプログラムは、最初のコマンドライン引数を使用しています

import java.util.regex.*; 
class Regex { 
    public static void main(String [] args) { 
    Pattern p = Pattern.compile(args[0]); 
    Matcher m = p.matcher(args[1]); 
    System.out.println("Pattern is " + m.pattern()); 
    while(m.find()) { 
     System.out.println(m.start() + " " + m.group()); 
    } 
    } 

}

...あなたのコードが、パターンを使用する方法の例を与えていない(argsが[0])表現します使用する正規表現の を使用し、2番目の引数(args [1])を使用して を検索するソースデータを表します。ここでテストの実行は次のとおり

Javaの正規表現「56」「AB4 56_7ab」 は出力を生成します

​​
0

ヒント:我々は、(すべての出発位置の付加)を割ることによって平均開始位置を計算する(総数でここでは)

をキーワードを含む文章のあなたのためのコードです:

import java.util.Scanner; 

/** 
* Program to calculate the average starting position of a given keyword in user input sentences. 
* We calculate average starting position by dividing (addition of all starting positions) by (total number of sentences containing keyword) 
* @author Garbage 
* 
*/ 
public class Lab6Loops { 

    public static void main(String[] args) { 

     String keywordString; 
     String inputString; 

     Scanner keyboard = new Scanner(System.in); 

     int totalSentences = 0; 
     int interestingSentences = 0; 
     int positionTotal = 0; 

     System.out.print("Enter a keyword. We will search each sentence for this word. : "); 
     keywordString = keyboard.nextLine(); 

     System.out.println("Please enter a sentence or type 'stop' to finish"); 
     inputString = keyboard.nextLine(); 

     while (!inputString.equals("stop")) { 
      totalSentences = totalSentences + 1; //totalSentences++ 

      // Check if the sentence contains our keyword 
      if (inputString.contains(keywordString)){ 
       interestingSentences = interestingSentences + 1; // This is sentence we would like to check 
       positionTotal = positionTotal + inputString.indexOf(keywordString); // Add the starting position to our total 
      } 
      System.out.println("Enter a line of text or 'stop' to finish"); 
      inputString = keyboard.nextLine(); 
     } 
     System.out.println("You entered " + totalSentences + " sentences"); 
     System.out.println("You have " + interestingSentences + "sentences that contain the keyword "+keywordString); 
     System.out.println("The average starting position is "+(positionTotal/interestingSentences)); 
    } 
}