Search code examples
javaarraysloopsoutput

Why does my Loop not repeat after inputting a correct value from an String Array?


I'm creating a basic word puzzle program as a personal project where a user sees a set of letters and has to create as many words from that set of letters (ie. I T E R; rite, tire, er). A score is added up for each time they get a correct word from one of the matches in a String array. If an incorrect guess is made (the guess does not appear in the array), a final score and the time it took are displayed. My problem is that after I input a value (re) and then enter another correct value (tire), the loop doesn't allow me to add another guess but exits the loop, and the score only gets updated to the first correct guess, not both. How can i change the logic so that i can enter any of the words from the array list in any order and get the correct score?

I tried to move the cont boolean outside the for loop. I tried to add the cont boolean in an else if statement when checking the if array item does not equal the input. Same issue persisted, i can enter re then tire and the program stops. Here is my code.

import java.sql.SQLOutput;
import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Instant starts = Instant.now();
        String guess;

        int score = 0;
        Scanner input = new Scanner(System.in);
        String seconds;
        String[] puzzle1 = {"iter", "rite", "tier", "tire", "trie",
                "ire", "rei", "ret", "rit", "tie", "er", "et", "re", "te", "ti"};
        Boolean cont = true;

        System.out.println("How many words can you create with the following letters:" +
                "\n  T   I   E   R");
        System.out.println("Enter a guess: ");
        String userInput = input.nextLine();
        int k= 0;
        while (cont) {

            for (int i = 0; i < puzzle1.length; i++) {

                if (puzzle1[i].equals(userInput)) {
                    score += 100;
                    System.out.println("Good! Enter another guess: ");
                    userInput = input.nextLine();
                }

            }
cont = false;

        }

        Instant ends = Instant.now();
        long mins = Duration.between(starts, ends).toMinutes();
        long time = Duration.between(starts, ends).toSeconds();
        long actual = time % 60;


        if (time <= 9) {
            seconds = "0" + actual;
        } else {
            seconds = String.valueOf(time);
        }

        System.out.println("Your time was " + mins + ":" + seconds + " and with a score of " + score + " points.");

    }}

here is the output

How many words can you create with the following letters:
  T   I   E   R
Enter a guess: 
re
Good! Enter another guess: 
tire
Your time was 0:05 and with a score of 100 points.

Solution

  • It seems like you just need to figure out your logic around cont = false, as in, what would you want to make this program terminate, a wrong answer maybe?

    Keep cont = true until you're ready to leave the loop. Also, you may want to consider using a List<String> instead of String[], this allows you to remove the nested for-loop in favor of the contains method.

    e.g. replace

    for (int i = 0; i < puzzle1.length; i++) {
       if (puzzle1[i].equals(userInput)) {
          score += 100;
          System.out.println("Good! Enter another guess: ");
          userInput = input.nextLine();
       }
    }
    

    with simply

    if(puzzle.contains(userInput)) {
       score += 100;
       System.out.println("Good! Enter another guess: ");
       userInput = input.nextLine();
    } else {
       System.out.println("Terrible!: ");
       cont = false;
    }