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

 Live Demo

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.

Updated on: 13-Jul-2020

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements