Learn Java Basics
About Lesson

Taking user inputs in Java


import java.util.*;
public class InputExample
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// Input a number
System.out.print("Enter a number: ");
int number = in.nextInt();
// Input a character
System.out.print("Enter a character: ");
char character = in.next().charAt(0);
// Input a floating number
System.out.print("Enter a floating number: ");
float floatNumber = in.nextFloat();
// Input a string
System.out.print("Enter a string: ");
String string = in.next();
// Display the inputs
System.out.println("You entered: ");
System.out.println("Number: " + number);
System.out.println("Character: " + character);
System.out.println("Floating number: " + floatNumber);
System.out.println("String: " + string);
}
}

Here is an explanation of each word in the given code:

import java.util.*; – This line imports all the classes from the java.util package. The Scanner class is part of this package.

public class InputExample – This line declares a class named InputExample.

public static void main(String[] args) – This line declares the main method that serves as the entry point for the program. It takes an array of strings as input.

Scanner in = new Scanner(System.in); – This line creates an object of the Scanner class and assigns it to the variable in. The Scanner class is used to read user input from the console.

System.out.print("Enter a number: "); – This line displays the message “Enter a number:” on the console.

int number = in.nextInt(); – This line reads an integer from the console and assigns it to the variable number.

char character = in.next().charAt(0); – This line reads a character from the console and assigns it to the variable character.

float floatNumber = in.nextFloat(); – This line reads a floating-point number from the console and assigns it to the variable floatNumber.

String string = in.next(); – This line reads a string from the console and assigns it to the variable string.

System.out.println("You entered: "); – This line displays the message “You entered:” on the console.

System.out.println("Number: " + number); – This line displays the value of the variable number on the console.

System.out.println("Character: " + character); – This line displays the value of the variable character on the console.

System.out.println("Floating number: " + floatNumber); – This line displays the value of the variable floatNumber on the console.

System.out.println("String: " + string); – This line displays the value of the variable string on the console.

} – This line closes the main method.

} – This line closes the InputExample class.

Program to Calculate Sum of Two Numbers

import java.util.*;
public class SumOfTwoNumbers
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int num1, num2, sum;
// Input first number
System.out.print("Enter first number: ");
num1 = in.nextInt();
// Input second number
System.out.print("Enter second number: ");
num2 = in.nextInt();
// Calculate sum
sum = num1 + num2;
// Display the result
System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum); 
}
}

Explain

  • import java.util.*;: This line imports the entire java.util package, which contains the Scanner class used to read input from the user.
  • public class SumOfTwoNumbers: This line defines a new public class called SumOfTwoNumbers.
  • public static void main(String[] args): This line defines the main() method, which is the entry point for the Java program.
  • Scanner in = new Scanner(System.in);: This line creates a new Scanner object called in, which will be used to read input from the user via the standard input stream.
  • int num1, num2, sum;: This line declares three integer variables called num1, num2, and sum.
  • System.out.print("Enter first number: ");: This line outputs the message “Enter first number:” to the console.
  • num1 = in.nextInt();: This line reads an integer input from the user using the nextInt() method of the Scanner class and assigns it to the variable num1.
  • System.out.print("Enter second number: ");: This line outputs the message “Enter second number:” to the console.
  • num2 = in.nextInt();: This line reads an integer input from the user using the nextInt() method of the Scanner class and assigns it to the variable num2.
  • sum = num1 + num2;: This line calculates the sum of the variables num1 and num2 and assigns the result to the variable sum.
  • System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);: This line outputs the message “Sum of num1 and num2 is: sum” to the console using the println() method of the System class.


Join the conversation