Algorithm
Input Example in Java -
- Create an instance of
Scanner
class - Access Scanner class input data as
int
Scanner input = new Scanner(System.in); // Create an instanceof Scanner class in input
int number = input.nextInt(); // Take input from input Instance and put that in number variable
Output Example in Java -
System.out.print()
is the method by which we can print anything in Java Programming Language.
System.out.print("Welcome to Java World");
We can Print Dynamic variable in Java Programming Language
int number = 100;
System.out.println("Number entered: " + number); // Number entered: 100
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner; // Library to support input
public class Main {
public static void main(String[] args){
// Create an instanceof Scanner class in input
Scanner input = new Scanner(System.in);
// Print function System.out.print()
System.out.print("Enter a number: \n");
int number = input.nextInt(); // Take input from input Instance
// Print anything with variable concating
System.out.println("Number entered: " + number);
}
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Working with many types input in Java Programming
Code -
Java Programming
import java.util.Scanner; // Library to support input
public class Main {
public static void main(String[] args){
// Create an instanceof Scanner class in input
Scanner input = new Scanner(System.in);
// Print function System.out.print()
System.out.print("Enter int: ");
int number = input.nextInt(); // Take input from input Instance
System.out.println("Int value is: " + number);
// Get float type input
System.out.print("Enter float: ");
float floatValue = input.nextFloat();
System.out.println("Float value is: " + floatValue);
// Get double type input
System.out.print("Enter double: ");
double doubleValue = input.nextDouble();
System.out.println("Double value is: " + doubleValue);
// Get String type input
System.out.print("Enter text: ");
String stringValue = input.next();
System.out.println("Text value is: " + stringValue);
}
}
Copy The Code &
Try With Live Editor
Input
Enter float: 10.12
Enter double: 90.121212
Enter text: Welcome
Output
Float value is: 10.12
Double value is: 90.121212
Text value is: Welcome
Demonstration
All available Scanner methods in JAVA -
int nextInt() | It is used to scan the next token of the input as an integer. |
float nextFloat() | It is used to scan the next token of the input as a float. |
double nextDouble() | It is used to scan the next token of the input as a double. |
byte nextByte() | It is used to scan the next token of the input as a byte. |
String nextLine() | Advances this scanner past the current line. |
boolean nextBoolean() | It is used to scan the next token of the input into a boolean value. |
long nextLong() | It is used to scan the next token of the input as a long. |
short nextShort() | It is used to scan the next token of the input as a Short. |
BigInteger nextBigInteger() | It is used to scan the next token of the input as a BigInteger. |
BigDecimal nextBigDecimal() |
It is used to scan the next token of the input as a BigDecimal. |
Please follow the concept in the Algorithm section and it's described there.