To read user input in a Java program we can use the,
1. String args[] : Command Line Arguments
These are command-line arguments that have to be passed when the Java program is run on command prompt/terminal.
$ java myProg Args1 Args2
String args is an array of String values. In the above example Arg1 = args 0 element, Args2 = args 1 element of the String[] array,
Code Example:import java.util.Scanner;
public class stringArgsDemo {
public static void main(String[]arguments){
//Java Scanner String Input
Scanner input = new Scanner(System.in);
String name;
int age;
System.out.print("Enter First multiple");
number1 = input.nextInt();
//This is not an interactive way!
name = args[0];
age = Integer.parseInt(args[1]);
System.out.println("Name : "+name +"\n" +"Age : "+age);
}
}
2. Scanner : Standard Input
Use Scanner Class in Java as an interactive way of accepting inputs from the user. Whenever input.nextInt(), input.next() is encountered the prompt stops to get input from the user.
Code Example:
import java.util.Scanner;
public class scannerClassDemo {
public static void main(String[]arguments){
//java scanner input
Scanner input = new Scanner(System.in);
String name;
int age;
System.out.print("Enter First multiple");
number1 = input.nextInt();
//Interactive way of getting input
System.out.printlin ("Enter your Name : ");
name = input. next();
System.out.printlin ("Enter your Age : ");
age = input. nextInt();
System.out.println("Name : "+name +"\n" +"Age : "+age);
}
}
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- trurl: A new command-line tool for URL parsing and manipulation by cURL Developer - cURL
- Add Bookmark macOS Safari - MacOS
- Save TextEdit file as a PDF - MacOS
- Android : DeviceMonitor] Sending Tracking request failed! Error - Android
- How to install PowerShell on Mac using Brew - Powershell
- Create Nested Directories using Java Code - Java
- bash get year 2021 calendar - Bash
- Calculate Volume of Ellipsoid - C-Program