Jumble Word Game in Java

In Jumble word game, the computer program randomly picks up a secret word from a database of words and then shuffles it. The shuffled word is presented to the user and he is asked to guess the original word. The program ends when the user correctly guesses the word. The program also prints the number guesses made by the user for finding the correct answer.

The following Java program implements the Jumble word game. This program demonstrates a number of important language features such as getting user input, conditional checks, loops and string operations. It also demonstrates how a random word is picked up using the ThreadLocalRandom class, how a word is shuffled using array operations and how Scanner class can be used for user input.

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

// How to create a Jumble word game in Java

public class JumbleGame {

    private static final String[] WORDS_DATABASE = new String[] {
    public static void main(String[] args) {
        JumbleGame jg = new JumbleGame();

     * Run a game of Jumble in Java. The steps in the game are,
     * 1. Get a random word from the words database
     * 2. Shuffle/jumble the word by randomly shuffling characters
     * 3. Present the jumbled word to the user and ask him to guess the word.
     * 4. Repeat the guess till answer is found or user decides to quit.
    private void startGame() {
        int numberOfGuesses = 0;
        String original = selectRandomWord();
        String shuffled = getShuffledWord(original);
        boolean gameOn = true;
        while(gameOn) {
            System.out.println("Shuffled word is: "+shuffled);
            String userGuess = getUserGuess();
            if(original.equalsIgnoreCase(userGuess)) {
                System.out.println("Congratulations! You found the word in "+numberOfGuesses+" guesses");
                gameOn = false;
            }else {
                System.out.println("Sorry, Wrong answer");
     * Get the user's word guess from command line
     * @return 
    public String getUserGuess() {
        Scanner sn = new Scanner(System.in);
        System.out.println("Please type in the original word: ");
        return sn.nextLine();
     * Select a random word from the WORDS_DATABASE array.
     * @return 
    public String selectRandomWord() {
        int rPos = ThreadLocalRandom.current().nextInt(0, WORDS_DATABASE.length);
        return WORDS_DATABASE[rPos];
     * Shuffle the original word by randomly swapping characters 10 times
     * @param original
     * @return 
    public String getShuffledWord(String original) {
        String shuffledWord = original; // start with original
        int wordSize = original.length();
        int shuffleCount = 10; // let us randomly shuffle letters 10 times
        for(int i=0;i<shuffleCount;i++) {
            //swap letters in two indexes
            int position1 = ThreadLocalRandom.current().nextInt(0, wordSize);
            int position2 = ThreadLocalRandom.current().nextInt(0, wordSize);
            shuffledWord = swapCharacters(shuffledWord,position1,position2);
        return shuffledWord;

     * Swaps characters in a string using the given character positions
     * @param shuffledWord
     * @param position1
     * @param position2
     * @return 
    private String swapCharacters(String shuffledWord, int position1, int position2) {
        char[] charArray = shuffledWord.toCharArray();
        // Replace with a "swap" function, if desired:
        char temp = charArray[position1];
        charArray[position1] = charArray[position2];
        charArray[position2] = temp;
        return new String(charArray);

Here are a number of ideas to improve the above program,

  • Modify the program so that the user can resign so that the computer will print the correct answer.
  • Modify the program so that you can play the game for multiple words at a time.

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution on this site within 48 hours! Please email us.

Leave a Reply