Reset Counter to Zero in Java?

user3945361

I have the following code:

for(int sentenceCounter = 0; sentenceCounter < sentences.length; sentenceCounter++)
{
    double score = 0;
    String[] sentence = sentences[sentenceCounter].split(" ");

    for (int titleCounter = 0; titleCounter < titles.length; titleCounter++)
    {

        for (int wordCounter = 0; wordCounter < sentence.length; wordCounter++)
        {
            if (titles[titleCounter].equals(sentence[wordCounter]))
            {
                if(titleCounter == 0)
                {

                    score += titleValue * hundred / 100;
                    sentenceScore.put(sentenceCounter, score);
                    System.out.println(sentenceCounter + " " + wordCounter + " " + score);

                }else
                {

                    score +=  titleValue - titleSubtract * hundred / 100;
                    sentenceScore.put(sentenceCounter, score);
                    System.out.println(sentenceCounter + " " + wordCounter + " " + sentenceScore.get(0));
                }

                System.out.println(sentenceCounter);

            }

        }
    }
}

I am trying to reset the score counter after the sentenceCounter increases, however when I set score = 0 at the right after the for loop of sentenceCounter it does not reset itself. So, how do I reset it?

The steps of the program is as follows: 1. Break title into single words. 2. Break sentence into single words. 3. See if sentence contains word from title. If yes then increase score of sentence by X. 4. Repeat for next word in title. 5. If sentence contains another word from title add to previous score. 6. Repeat this for next sentence but first set score counter to zero. This is where I am stuck.

This is the output I am observing from above code:

0 0 10.0
0
0 1 19.3
0
1 5 19.3
1

As you can see sentence two starts at 19.3 instead of starting at zero.

Here is the program in full:

import java.util.HashMap;

import java.util.Map;

public class MainDemo {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String fullTitle = "Facebook traffic website";
    String[] titles = fullTitle.split(" ");
    Double titleValue = 10.0;
    Double sentenceSubtract = 13.0;
    Double titleSubtract = 0.70;
    Double hundred = 100.0;
    HashMap<Integer, String> titleArray = new HashMap<Integer, String>();
     for(int i =0; i < titles.length; i++)
     {
         String t = titles[i];
         titleArray.put(i, t);
     }
    String article = "Facebook traffic is growing fast. It is the second largest website. ";

    HashMap<Integer, Double> sentenceScore = new HashMap<Integer, Double>();


    String[] sentences = article.split("\\. ");
    String[] str;



    for(int sentenceCounter = 0; sentenceCounter < sentences.length; sentenceCounter++)
    {
        double score = 0;
        String[] sentence = sentences[sentenceCounter].split(" ");

        for (int titleCounter = 0; titleCounter < titles.length; titleCounter++)
        {



            for (int wordCounter = 0; wordCounter < sentence.length; wordCounter++)
            {
                if (titles[titleCounter].equals(sentence[wordCounter]))
                {
                    if(titleCounter == 0)
                    {

                        score += titleValue * hundred / 100;
                        sentenceScore.put(sentenceCounter, score);
                        System.out.println(sentenceCounter + " " + wordCounter + " " + score);

                    }else
                    {

                        score +=  titleValue - titleSubtract * hundred / 100;
                        sentenceScore.put(sentenceCounter, score);
                        System.out.println(sentenceCounter + " " + wordCounter + " " + sentenceScore.get(0));
                    }

                    System.out.println(sentenceCounter);

                }

            }
        }
    }





        }















}
jabgibson

Your score counter is being reset every iteration. The problem is that you are printing a different set of values if titleCounter != 0. The print to console that you are expecting to print score, is actually defined to print sentenceScore.get(0).

This is correct. sentenceScore.get(0) would be the score value from last iteration before it was reset to 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related