create input in java code example

Example 1: how to input in java

import java.util.Scanner;
...
  Scanner console = new Scanner(System.in);
  int num = console.nextInt();
  console.nextLine() // to take in the enter after the nextInt() 
  String str = console.nextLine();

Example 2: user input in java

import java.util.Scanner;  // Import the Scanner class

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output user input
  }
}

Tags:

Dart Example