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,
- How to get Java Thread name Example [Program]
- Java 8: Get First and Last Date of the Week for Given Date
- Convert String to int in Java
- How to Get List of All Country Codes in Java Using Locale Class
- Convert Multidimensional Array toString In Java
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Create a Zip file using Java Code programmatically
- [Fix] java.net.MalformedURLException: unknown protocol
- [fix] Java JDBC ConnectException: Connection refused
- Read Json File and Convert to Java Object using Jackson
- list of jars required for hibernate 4.x.x
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java: The value of the local variable string is not used
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Run SQL Script file using Java JDBC Code Example
- Remove Trailing zeros BigDecimal Java
- Java JDBC IN Clause Example with PreparedStatement MySQL
- Convert Java List to Json String using Jackson
- Java 8 foreach loop code examples
- error: file not found: HelloWorld.java
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Struts 2 Hello World Example in Eclipse
More Posts:
- Cant find System Preferences... under macOS Ventura Apple Menu? - MacOS
- Enable Dark Mode in Gmail Desktop Browser - Google
- Meaning of javascript:void(0) explained with example - JavaScript
- Enable and reduce background noise in Microsoft Teams with AI-based noise suppression - Teams
- Using Document Map in Notepad++ - NotepadPlusPlus
- How to integrate Salesforce CRM Sales and Service with Microsoft Teams - Teams
- [fix] RabbitMQ: AuthenticationFailureException ACCESS_REFUSED Login refused authentication mechanism PLAIN - 2022
- How to use Autocomplete and Autosuggestion in Shell Commands - Bash