Ad

Taking Two Inputs Of Two Different Data Types In A Single Line In Java

- 1 answer

In Codechef and similar sites, the inputs are taken in a single line.While taking two integer inputs in single line is no issue.But how can i can take a string input and a long input in a single line in java.Because if i enter the String first and after giving a space,I enter the the long number, won't the total line be considered as a string.So my question is how can i take a string input and a long input in a single line in Java? An example -(Source-Codeforces) 5 //no of test cases Jett 012345678 //String input and long input in a single line. Viper 111111111 Neon 987654321 Raze 512610294 Reyna 192830492

Ad

Answer

import java.util.Scanner;

public class MainApplication{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String input1 = scanner.next();
        long input2 = scanner.nextLong();
        System.out.println("input1:" + input1);
        System.out.println("input2:" + input2);
    }
}

> javac MainApplication.java
> java MainApplication
viper 111111111
input1: viper
input2: 111111111

You can use next() first to capture the String datatype, then nextLong() to capture Long datatype.

Ad
source: stackoverflow.com
Ad