- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to input multiple values from user in one line in Java?
To input multiple values from user in one line, the code is as follows −
Example
import java.util.Scanner; public class Demo { public static void main(String[] args) { System.out.print("Enter two floating point values : "); Scanner my_scan = new Scanner(System.in); double double_val = my_scan.nextFloat(); int int_val = my_scan.nextInt(); System.out.println("The floating point value is : " + double_val + " and the integer value is : " + int_val); } }
Input
56.789 99
Output
Enter two floating point values : The floating point value is : 56.78900146484375 and the integer value is : 99
A class named Demo contains the main function, inside which a Scanner class object is created, and two values, one double and one integer values are parsed. The values are taken from the standard input and then displayed on the console.
The Scanner object parses the input directly, without splitting it into multiple strings. In addition to this, the user can input the data on a single line or on multiple lines.
Advertisements