Java

 

 

This program allows you to test your typing speed by comparing the time it takes to type a given passage accurately. The program calculates the number of words per minute (WPM) based on the time and accuracy.

Objective:

The goal of this Typing Speed Test Application is to evaluate a user’s typing speed in terms of words per minute (WPM). The program provides a random text to type, records the time taken by the user, and then calculates the typing speed by comparing the typed text to the original text.

Java Code for Typing Speed Test:

public class TypingSpeedTest {
    import java.util.Scanner;

    public class TypingSpeedTest {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            // Define the passage to type
            String passage = "The quick brown fox jumps over the lazy dog.";
            
            System.out.println("Typing Speed Test");
            System.out.println("Type the following passage:");
            System.out.println(passage);
            
            // Start the timer
            long startTime = System.currentTimeMillis();
            
            // Take user input for the passage
            System.out.println("\nStart Typing: ");
            String userInput = scanner.nextLine();
            
            // End the timer
            long endTime = System.currentTimeMillis();
            
            // Calculate time taken to type
            long timeTaken = endTime - startTime; // in milliseconds
            
            // Calculate the words per minute (WPM)
            int wordCount = userInput.split("\\s+").length;
            double timeTakenInMinutes = timeTaken / 60000.0; // convert time to minutes
            double wpm = wordCount / timeTakenInMinutes;
            
            // Calculate accuracy
            int correctChars = 0;
            for (int i = 0; i < Math.min(passage.length(), userInput.length()); i++) {
                if (passage.charAt(i) == userInput.charAt(i)) {
                    correctChars++;
                }
            }
            double accuracy = (double) correctChars / passage.length() * 100;
            
            // Display results
            System.out.println("\nTime taken: " + timeTaken / 1000 + " seconds");
            System.out.println("Words per minute: " + wpm);
            System.out.println("Accuracy: " + accuracy + "%");
            
            scanner.close();
        }
    }

Explanation of the Program:

This program measures typing speed by following these steps:

  1. Passage Selection: The program provides a predefined text (“The quick brown fox jumps over the lazy dog”) that the user must type.
  2. Timing: The time is recorded using System.currentTimeMillis() both at the beginning and the end of the typing session to calculate the typing duration.
  3. Words Per Minute (WPM): WPM is calculated by dividing the number of words typed by the time taken in minutes.
  4. Accuracy Calculation: The program compares each character typed by the user with the corresponding character in the original passage to determine accuracy.
  5. Result Display: The program displays the time taken, words per minute, and accuracy as output.

How to Run the Program:

  1. Ensure you have the Java Development Kit (JDK) installed on your machine.
  2. Copy the code into a file named TypingSpeedTest.java.
  3. Open a terminal (or command prompt) and navigate to the directory where the file is saved.
  4. Compile the code using the following command:
    javac TypingSpeedTest.java
  5. Run the program using the following command:
    java TypingSpeedTest
  6. Follow the on-screen instructions to start typing the provided passage and get your typing results.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)