Home > Software design >  Same input keeps adding to the arraylist
Same input keeps adding to the arraylist

Time:06-14

I'm writing a number guessing game in java. Number guessing game is a numeric version of famous hangman, where computer picks a number between a prespecified range and user has to guess that number. Requirements:

  1. User must guess a number between 0-1000 and tells the user the range of guessed number.
  2. User has max 10 guesses.
  3. Every time user makes a guess, total guesses reduce by one.
  4. Computer keeps track of all the numbers user has guessed so far and shows this information before next guess.
  5. If the guess is correct, game ends in a win. In case of incorrect guess, computer gives a hint to the user. If the user guess is greater than the picked number, then client tell the user that ‘your guess is bigger’ and in case of being smaller appropriate message is shown.
  6. In case of invalid guess (alphabets, symbols and repeated guesses) one warning is given and on next warning user loses a guess

The following code is running fine but it always shows the same number after guesses number. I think its not adding the new input in the arrraylist rather the first one everytime.

    import java.util.*;
import java.lang.*;
public class NumberGuess {
    public static void main(String[] args) {


        int tries = 10;
        ArrayList<Integer> guessed = new ArrayList();
        int warnings = 2;
        int i = 0;
        Random rand = new Random();
        int random = rand.nextInt(1000);
        private void StartMenu () {
            System.out.println("\" Welcome to the Number guessing game!\n I am thinking of a number between 0-1000\n You have 1 warning.\n You have 1 warning.\n ------------ ");
        }

        public char[] ToCharacterArray (String input){
            char arr[] = new char[input.length()];
            arr = input.toCharArray();
            return arr;

        }

        public boolean CheckInput ( char arr[]){
            if (Character.isDigit(arr[0])) {
                return true;
            } else {

                return false;
            }
        }


        String input;

        while (tries > 0 && warnings > 0) {
            System.out.println("You have "   tries   " guesses left.");
            if (tries == 10) {
                System.out.println("guessed number: ");
            } else {
                System.out.println("guessed number: ");
                for (Integer a : guessed) {
                    System.out.println(guessed.get(i));
                }
            }
            System.out.println("Please guess a number:");
            Scanner sc = new Scanner(System.in);

            input = sc.next();

            char InputString[] = ToCharacterArray(input);
            if (CheckInput(InputString)) {
                int intInput = Integer.parseInt(input);
                guessed.add(intInput);
                if (intInput > random) {
                    System.out.println("Your guess is greater");
                }
                if (intInput < random) {
                    System.out.println("Your guess is smaller");
                }
                if (intInput == random) {
                    System.out.println("Congrats! You win.");
                    System.out.println("The guessed number is: "   intInput);
                    tries = -1;
                }

            }
            tries--;


        }
    }
}

CodePudding user response:

The problem is because you iterate over the guessed numbers, and then print out the item of index i from that list. The number i at that point will always be zero, so it will always print just the first element from that list. Instead, you can just print the a, that is the element itself, after the iteration on guessed. Here is how it will look like:

System.out.println("guessed number: ");
for (Integer a : guessed) {
    System.out.println(a);
}

CodePudding user response:

You actually have a lot of problems with this code. I cleaned it up a bit:

public static void main(String[] args) {
    List<String> guessed = new ArrayList<>();
    int random = new Random().nextInt(1000);
    System.out.println("\" Welcome to the Number guessing game!\n I am thinking of a number between 0-1000\n You have 1 warning.\n You have 1 warning.\n ------------ ");
    try (Scanner sc = new Scanner(System.in)) {
        for (int tries = 10; tries >= 0; tries--) {
            System.out.println("You have "   tries   " guesses left.");
            
            if (!guessed.isEmpty()) {
                System.out.println("Guessed numbers: "   String.join(", ", guessed));
            }
            
            System.out.println("Please guess a number:");
            
            String input = sc.next().trim();

            try {
                int value = Integer.parseInt(input);
                if (value > random) {
                    System.out.println("Your guess is greater");
                } else if (value < random) {
                    System.out.println("Your guess is smaller");
                } else {
                    System.out.println("Congrats! You win.");
                    break;
                }
                guessed.add(input);
            } catch (Exception e) {
                System.out.println("Value is not an intenger, try again.");
            }
        }
        System.out.println("The number was: "   random);
    }
}
  • Related