Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Taking two inputs of two different data types in a single line in Java

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading