Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Word Points Game keeps adding numbers

public static void pointsProblem() throws java.io.FileNotFoundException {
    FileInputStream pp = new FileInputStream("dictionary.txt");
    Scanner dictionaryIn = new Scanner(pp);
    Scanner input = new Scanner (System.in);
    System.out.println("Points Problem.");
    
    char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    int[] points = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
    int score = 0;
    String word; 
    while (dictionaryIn.hasNextLine()) { 
        word = dictionaryIn.nextLine();
        for (int i = 0; i < word.length(); i++) {
            for (int n = 0; n < 26; n++) {
                if (word.charAt(i) == alphabet[n]) { 
                score += points[n];
            }
            }
        }
        System.out.println(word + "is worth " + score + " points.");
    }
    System.out.println("");
}

In my point game i have a txt file with words on it. Everytime i run the game all of the word points get added up to the next word. I want each word to start from 0 not from the appointed points from previous words.

Here is the output

Points Problem.
passersbyis worth 16 points.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

absobloominglutelyis worth 44 points.

nanais worth 48 points.

bananais worth 56 points.

theis worth 62 points.

quickis worth 82 points.

brownis worth 92 points.

foxis worth 105 points.

jumpsis worth 121 points.

overis worth 128 points.

theis worth 134 points.

lazyis worth 150 points.

dogis worth 155 points.

ais worth 156 points.

mmis worth 162 points.

wowis worth 171 points.

noonis worth 175 points.

radaris worth 181 points.

redderis worth 189 points.

racecaris worth 200 points.

redivideris worth 214 points.

aibohphobiais worth 237 points.

tattarrattatis worth 249 points.

>Solution :

Easy peasy!

Move

int score = 0;

Inside your while loop so that it gets reset every time!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading