Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Keyboard Input and Random Number Generation in Java

Tech 1

Handling Keyboard Input with Scanner

To capture different data types from keyboard input in Java, use the Scanner class following these steps:

  1. Import the Scanner class: import java.util.Scanner;
  2. Create a Scanner object: Scanner input = new Scanner(System.in);
  3. Use appropriate methods to read differnet data types:
    • input.next() for String
    • input.nextInt() for integer
    • input.nextDouble() for double
    • input.nextBoolean() for boolean
  4. Close the scanner: input.close();

Example Implementation:

import java.util.Scanner;

public class UserInputExample {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Welcome to our registration system!");
        System.out.print("Enter username: ");
        String username = input.next();

        System.out.print("Enter age: ");
        int age = input.nextInt();

        System.out.print("Enter weight: ");
        double weight = input.nextDouble();

        System.out.print("Are you single (true/false): ");
        boolean isSingle = input.nextBoolean();

        System.out.print("Enter gender (M/F): ");
        char gender = input.next().charAt(0);

        System.out.println("\nRegistration details:");
        System.out.println("Username: " + username);
        System.out.println("Age: " + age);
        System.out.println("Weight: " + weight);
        System.out.println("Single: " + isSingle);
        System.out.println("Gender: " + gender);
        
        input.close();
    }
}

Ganerating Random Numbers

To generate random numbers within a specific range:

  1. Use Math.random() wich returns a double between 0.0 (inclusive) and 1.0 (exclusive)
  2. For a range [a, b], use: (int)(Math.random() * (b - a + 1)) + a

Example Implementation:

public class RandomNumberGenerator {
    public static void main(String[] args) {
        // Generate random number between 1 and 6
        int diceRoll = (int)(Math.random() * 6) + 1;
        System.out.println("Dice roll: " + diceRoll);
        
        // Generate random number between 10 and 20
        int randomInRange = (int)(Math.random() * 11) + 10;
        System.out.println("Random between 10-20: " + randomInRange);
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.