Java Project – Typing Speed Test

We offer you a brighter future with FREE online courses - Start Now!!

Welcome to the Typing Speed Test! Are you ready to put your typing skills to the test and see how fast and accurate you can type? This interactive program will measure your typing speed accuracy, and even provide insights into your performance.

Remember, the Typing Speed Test is a great way to assess and enhance your typing skills. Whether you’re a professional seeking to improve productivity or simply want to impress your friends with your lightning-fast typing, this test will help you gauge your abilities.

So, are you ready to take on the challenge? Let’s embark on this typing adventure together and discover just how fast and accurate your fingers can be on the keyboard. Get ready to set new personal records and exceed your own expectations.

Throughout this typing adventure, you’ll witness your progress firsthand, as each session brings you closer to reaching new personal records and surpassing your own expectations. Are you prepared? Let the countdown begin!

About Java Typing Speed Test Project

We will implement the following functionalities in the Java Typing Speed Test Project :

1. Import the Scanner and Random classes from the java.util package to read user input and generate random numbers.
2. Define arrays of sentences and paragraphs that will be used in the typing speed test.
3. Create a main method to handle the test logic.
4. Display a welcome message for the user.
5. Set up a loop to allow the user to take the typing test multiple times.
6. Prompt the user to select the type of text they want to type (sentence or paragraph).
7. Randomly select a sentence or paragraph based on the user’s choice.
8. Display the selected text to the user and prompt them to press Enter to start the test.
9. Record the start time when the user starts typing.
10. Prompt the user to type the text.
11. Record the end time when the user finishes typing.
12. Calculate the elapsed time in seconds.
13. Compare the user’s input with the original text character by character to count the number of correct characters.
14. Calculate the accuracy of the user’s typing as a percentage.
15. Calculate the words per minute (WPM) based on the user’s input and elapsed time.
16. Display the test results, including time elapsed, accuracy, and WPM.
17. Check for extra or missing characters in the user’s input and display appropriate messages.
18. Prompt the user to try the test again. If they choose to restart, repeat the loop. Otherwise, exit the loop.
19. Display a farewell message and close the Scanner object to release system resources.

Overall, the code will allow us to take a typing speed test, measure our accuracy and speed, and provide test results and the option to retry the test.

Prerequisites For Java Typing Speed Test

To write and run the Java Typing Speed Test Project code, the following prerequisites are required:

1. Basic understanding of the Java programming language, including knowledge of loops, conditional statements, and object-oriented programming concepts.
2. Familiarity with array data structures and the Java.util package.
3. A working Java development environment, which includes installing the Java Development Kit (JDK) on your computer. You can download the JDK from the official Oracle website or use a package manager specific to your operating system.
4. An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or NetBeans, which can greatly simplify Java development. Choose an IDE you are comfortable with and set it up.

Download Java Typing Speed Test Project

Please download the source code of the Java Typing Speed Test Project: Java Typing Speed Test Project Code.

Code Breakdown

1. Import Statements, Class Definition and Array Declarations:

  • Import necessary classes Scanner and Random from the java.util package.
  • Define the class TypingSpeedTest to encapsulate the typing speed test functionality.
  • Declare two arrays, sentences and paragraphs, containing text samples for the typing test.
import java.util.Scanner;
import java.util.Random;

public class TypingSpeedTest {
    // Array of different sentences
    private static final String[] sentences = {
        "Your journey to excellence starts at Project Gurukul.",
        "Elevate learning experiences with Project Gurukul's resources.",
        "Discover, learn, and thrive with Project Gurukul's expertise.",
        "Project Gurukul's website hosts an impressive collection of diverse projects.",
        "Each project featured on Project Gurukul's website is thoughtfully documented.",
    };

    private static final String[] paragraphs = {
        "Welcome to Project Gurukul, your gateway to a world of hands-on learning and innovation. Explore our website's diverse collection of projects, each accompanied by its source code. Embark on a journey of practical discovery with Project Gurukul's rich resource of project-based learning.",
        "Project Gurukul's website brings you projects backed by source code. It's like peeking behind the scenes of software and learning from the inside out. Dive in, have fun, and level up your coding skills.",
        "Ever wondered how to turn coding theory into practice? Project Gurukul has your answer. On our website, projects come alive with source code you can study and use. It's a hands-on way to grasp coding concepts and make them your own.",
        "Join us at Project Gurukul and explore various projects with their source code. It's like having a peek behind the scenes of how things work. Dive in, learn, and have fun coding!",
        "Dive into Project Gurukul's website where you'll find projects along with their own source code. Aspiring developers can gain valuable insights into best practices and coding techniques by examining the well-commented source code available on Project Gurukul's website."
    };

2. main Method, Welcome Message and Setup:

  • The main Method acts as the primary starting point of the program, holding the main instructions for the typing speed test and user engagement.
  • A welcoming message is shown, and users are asked to pick between typing a sentence or a paragraph for the test.
public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      Random random = new Random();

      System.out.println("Welcome to the Typing Speed Test by ProjectGurukul!");
      System.out.println();

      while (true) {
          // Prompt the user to select the text type
          System.out.println("What type of text do you want to type?");
          System.out.println("1. Sentence");
          System.out.println("2. Paragraph");
          System.out.print("Enter your choice (1 or 2): ");
          int textTypeChoice = scanner.nextInt();
          scanner.nextLine(); // Consume the newline character left by nextInt()

          String originalText;

          if (textTypeChoice == 1) {
              // Randomly select a sentence from the array
              originalText = sentences[random.nextInt(sentences.length)];
          } else if (textTypeChoice == 2) {
              // Randomly select a paragraph from the array
              originalText = paragraphs[random.nextInt(paragraphs.length)];
          } else {
              System.out.println("Invalid choice. Please try again.");
              continue; // Restart the loop to get a valid choice
          }

3. Input Handling and Typing Test:

  • Take the user’s choice as input, and based on the input, randomly select a sentence or paragraph for the user to type.
  • After displaying the selected text, prompt the user to start the test. Calculate the elapsed time, number of correct characters, accuracy, and words per minute as the user types.
System.out.println("Type the following text:");
         System.out.println(originalText);
         System.out.println();

         // Prompt user to start the test
         System.out.println("Press Enter when you are ready to start the test.");
         scanner.nextLine();

         // User types the text
         System.out.println("Type the text:");
         long startTime = System.currentTimeMillis();
         String userInput = scanner.nextLine();
         long endTime = System.currentTimeMillis();

         // Calculate elapsed time in seconds
         long elapsedTime = endTime - startTime;
         double seconds = elapsedTime / 1000.0;

         int originalTextLength = originalText.length();
         int userInputLength = userInput.length();
         int correctCharacters = 0;

         // Compare the user's input with the original text to count correct characters
         for (int i = 0; i < Math.min(originalTextLength, userInputLength); i++) {
             if (originalText.charAt(i) == userInput.charAt(i)) {
                 correctCharacters++;
             }
         }

         // Calculate accuracy as a percentage
         int accuracy = (int) (((double) correctCharacters / originalTextLength) * 100);

         // Calculate words per minute
         double wordsPerMinute = (userInputLength / 5.0) / (seconds / 60);

4. Displaying Test Results:

  • Display the test results, including the time elapsed, accuracy, and words per minute.
  • Check for extra or missing characters and display relevant information.
// Display test results
       System.out.println();
       System.out.println("Test Result:");
       System.out.println("--------------");
       System.out.println("Time elapsed: " + seconds + " seconds");
       System.out.println("Accuracy: " + accuracy + "%");
       System.out.println("Words per minute: " + wordsPerMinute);

       // Additional functionality - Check for extra or missing characters
       if (userInputLength > originalTextLength) {
           int extraCharacters = userInputLength - originalTextLength;
           System.out.println("Extra characters typed: " + extraCharacters);
       } else if (userInputLength < originalTextLength) {
           int missingCharacters = originalTextLength - userInputLength;
           System.out.println("Missing characters: " + missingCharacters);
       }

       System.out.println("------------------------------");

5. Retry or Exit and Closing the Scanner:

  • After displaying the test results, ask the user if they want to try again. If the user chooses to retry, restart the typing test; otherwise, exit the program.
  • Close the Scanner after the loop ends to release resources.
 // Prompt the user to try again
            System.out.println("Would you like to try again? (Y/N)");
            String choice = scanner.nextLine();
            if (!choice.equalsIgnoreCase("Y")) {
                break; // Exit the loop if the user doesn't want to try again
            }
        }

        System.out.println("Thank you for using the Typing Speed Test by ProjectGurukul. Goodbye!");
        scanner.close(); // Close the scanner after the loop ends
    }
}

Code to illustrate the Java Typing Speed Test Project

import java.util.Scanner;
import java.util.Random;

public class TypingSpeedTest {
    // Array of different sentences
    private static final String[] sentences = {
        "Your journey to excellence starts at Project Gurukul.",
        "Elevate learning experiences with Project Gurukul's resources.",
        "Discover, learn, and thrive with Project Gurukul's expertise.",
        "Project Gurukul's website hosts an impressive collection of diverse projects.",
        "Each project featured on Project Gurukul's website is thoughtfully documented.",
    };

    private static final String[] paragraphs = {
        "Welcome to Project Gurukul, your gateway to a world of hands-on learning and innovation. Explore our website's diverse collection of projects, each accompanied by its source code. Embark on a journey of practical discovery with Project Gurukul's rich resource of project-based learning.",
        "Project Gurukul's website brings you projects backed by source code. It's like peeking behind the scenes of software and learning from the inside out. Dive in, have fun, and level up your coding skills.",
        "Ever wondered how to turn coding theory into practice? Project Gurukul has your answer. On our website, projects come alive with source code you can study and use. It's a hands-on way to grasp coding concepts and make them your own.",
        "Join us at Project Gurukul and explore various projects with their source code. It's like having a peek behind the scenes of how things work. Dive in, learn, and have fun coding!",
        "Dive into Project Gurukul's website where you'll find projects along with their own source code. Aspiring developers can gain valuable insights into best practices and coding techniques by examining the well-commented source code available on Project Gurukul's website."
    };

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        System.out.println("Welcome to the Typing Speed Test by ProjectGurukul!");
        System.out.println();

        while (true) {
            // Prompt the user to select the text type
            System.out.println("What type of text do you want to type?");
            System.out.println("1. Sentence");
            System.out.println("2. Paragraph");
            System.out.print("Enter your choice (1 or 2): ");
            int textTypeChoice = scanner.nextInt();
            scanner.nextLine(); // Consume the newline character left by nextInt()

            String originalText;

            if (textTypeChoice == 1) {
                // Randomly select a sentence from the array
                originalText = sentences[random.nextInt(sentences.length)];
            } else if (textTypeChoice == 2) {
                // Randomly select a paragraph from the array
                originalText = paragraphs[random.nextInt(paragraphs.length)];
            } else {
                System.out.println("Invalid choice. Please try again.");
                continue; // Restart the loop to get a valid choice
            }

            System.out.println("Type the following text:");
            System.out.println(originalText);
            System.out.println();

            // Prompt user to start the test
            System.out.println("Press Enter when you are ready to start the test.");
            scanner.nextLine();

            // User types the text
            System.out.println("Type the text:");
            long startTime = System.currentTimeMillis();
            String userInput = scanner.nextLine();
            long endTime = System.currentTimeMillis();

            // Calculate elapsed time in seconds
            long elapsedTime = endTime - startTime;
            double seconds = elapsedTime / 1000.0;

            int originalTextLength = originalText.length();
            int userInputLength = userInput.length();
            int correctCharacters = 0;

            // Compare the user's input with the original text to count correct characters
            for (int i = 0; i < Math.min(originalTextLength, userInputLength); i++) {
                if (originalText.charAt(i) == userInput.charAt(i)) {
                    correctCharacters++;
                }
            }

            // Calculate accuracy as a percentage
            int accuracy = (int) (((double) correctCharacters / originalTextLength) * 100);

            // Calculate words per minute
            double wordsPerMinute = (userInputLength / 5.0) / (seconds / 60);

            // Display test results
            System.out.println();
            System.out.println("Test Result:");
            System.out.println("--------------");
            System.out.println("Time elapsed: " + seconds + " seconds");
            System.out.println("Accuracy: " + accuracy + "%");
            System.out.println("Words per minute: " + wordsPerMinute);

            // Additional functionality - Check for extra or missing characters
            if (userInputLength > originalTextLength) {
                int extraCharacters = userInputLength - originalTextLength;
                System.out.println("Extra characters typed: " + extraCharacters);
            } else if (userInputLength < originalTextLength) {
                int missingCharacters = originalTextLength - userInputLength;
                System.out.println("Missing characters: " + missingCharacters);
            }

            System.out.println("------------------------------");

            // Prompt the user to try again
            System.out.println("Would you like to try again? (Y/N)");
            String choice = scanner.nextLine();
            if (!choice.equalsIgnoreCase("Y")) {
                break; // Exit the loop if the user doesn't want to try again
            }
        }

        System.out.println("Thank you for using the Typing Speed Test by ProjectGurukul. Goodbye!");
        scanner.close(); // Close the scanner after the loop ends
    }
}

Java Typing Speed Test Output

java Typing Speed Test

java Typing Speed Test project

Summary

In conclusion, the Java Typing Speed Test has provided you with a thrilling opportunity to gauge your typing skills, improve your speed, and enhance your accuracy. Through this interactive program, you have embarked on a journey of self-discovery and growth, uncovering your true potential as a typist.

Now, armed with newfound knowledge and determination, go forth and type with confidence. Embrace the joy of typing and let your fingers dance across the keys, transforming words into a symphony of productivity.

The world is at your fingertips, and the possibilities are endless. Keep typing, keep improving, and let your typing skills shine in all your future endeavors. Farewell, and happy typing!

Leave a Reply

Your email address will not be published. Required fields are marked *