'Taking two inputs of two different data types in a single line in Java [closed]

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 1:[1]

To get the string and long from one input you need to get the text and the number as a single String and then extract the number from the String. The following code shows how to get a long number from a text which are separated by a space:

public class Test{
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String input1 = scanner.nextLine();
        Long a = Long.parseLong(input1.substring(input1.indexOf(' ') + 1 ));
        System.out.println(a);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafa