Algorithm
1. Import the Scanner class from java.util package.
2. Create a class named GetInputs.
3. Inside the class, declare variables for an integer (a), a float (b), and a string (s).
4. Create a Scanner object named obj to read input from the console.
5. Prompt the user to enter a string and store it in the variable s.
6. Print the entered string.
7. Prompt the user to enter an integer and store it in the variable a.
8. Print the entered integer.
9. Prompt the user to enter a float and store it in the variable b.
10. Print the entered float
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
class GetInputs
{
public static void main(String args[])
{
int a;
float b;
String s;
Scanner obj = new Scanner(System.in); /* create a object */
System.out.println("Enter a string:");
s = obj.nextLine(); /* Take string input and assign to variable */
System.out.println("You entered string "+s); /* Print */
System.out.println("Enter an integer:");
a = obj.nextInt(); /* Take integer input and assign to variable */
System.out.println("You entered integer "+a); /* Print */
System.out.println("Enter a float:");
b = obj.nextFloat(); /* Take float input and assign to variable */
System.out.println("You entered float "+b); /* Print */
}
}
Copy The Code &
Try With Live Editor
Output
DevsEnv
You entered string DevsEnv
Enter an integer:
15
You entered integer 15
Enter a float:
12.5
You entered float 12.5
#2 Example: print() and println()
Code -
Java Programming
class Output {
public static void main(String[] args) {
System.out.println("1. println ");
System.out.println("2. println ");
System.out.print("1. print ");
System.out.print("2. print");
}
}
Copy The Code &
Try With Live Editor
Output
2. println
1. print 2. print
#3 Example: Printing Variables and Literals
Code -
Java Programming
class Variables {
public static void main(String[] args) {
Double number = -10.6;
System.out.println(5);
System.out.println(number);
}
}
Copy The Code &
Try With Live Editor
Output
-10.6
#4 Example: Print Concatenated Strings
Code -
Java Programming
class PrintVariables {
public static void main(String[] args) {
Double number = -10.6;
System.out.println("I am " + "DevsEnv.");
System.out.println("Number = " + number);
}
}
Copy The Code &
Try With Live Editor
Output
Number = -10.6
#5 Example: Get Integer Input From the User
Code -
Java Programming
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("You entered " + number);
// closing the scanner object
input.close();
}
}
Copy The Code &
Try With Live Editor
Output
You entered 25
#6 Example: Get float, double and String Input
Code -
Java Programming
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Getting float input
System.out.print("Enter float: ");
float myFloat = input.nextFloat();
System.out.println("Float entered = " + myFloat);
// Getting double input
System.out.print("Enter double: ");
double myDouble = input.nextDouble();
System.out.println("Double entered = " + myDouble);
// Getting String input
System.out.print("Enter text: ");
String myString = input.next();
System.out.println("Text entered = " + myString);
}
}
Copy The Code &
Try With Live Editor
Output
Float entered = 2.343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!
Demonstration
Java Programming to Get a Example User Input and Print-DevsEnv