Hacker rank : java variable Solution

Problem: 2
Scanner scanner = new Scanner(System.in);
String myString = scanner.next();
int myInt = scanner.nextInt();
scanner.close();

System.out.println("myString is: " + myString);
System.out.println("myInt is: " + myInt); 

The code above creates a Scanner object named  and uses it to read a String and an int. It then closes the Scanner object because there is no more input to read, and prints to stdout using System.out.println(String). So, if our input is: 

import java.util.*;


public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String myString = scanner.next();
        int myInt = scanner.nextInt();
        int a=scanner.nextInt();
        scanner.close();
        // Complete this line
        System.out.println(""+ myString);
       
        // Complete this line

        System.out.println(""+ myInt);
        // Complete this line
        // Complete this line
        System.out.println(""+ a);
    }
}
Output:



Post a Comment

Previous Post Next Post