Hackerrank Java Stdin and Stdout I Solution
1 min read

Hackerrank Java Stdin and Stdout I Solution

Hackerrank Java Stdin and Stdout I Solution

Most HackerRank challenges require you to read input from stdin (standard input) and write output to stdout (standard output).

One popular way to read input from stdin is by using the Scanner class and specifying the Input Stream as System.in. For example: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:

Hi 5

Our code will print:

myString is: Hi
myInt is: 5

Alternatively, you can use the BufferedReader class.

Task
In this challenge, you must read  integers from stdin and then print them to stdout. Each integer must be printed on a new line. To make the problem a little easier, a portion of the code is provided for you in the editor below.

Input Format

There are  lines of input, and each line contains a single integer.

Sample Input

42
100
125

Sample Output

42
100
125

Solution in java8

Approach 1.

import java.util.*;

public class Solution {

    public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      for(int i=0; i<3; ++i) {
         int a=sc.nextInt();
      System.out.println(a);
      } 
    }
}

Approach 2.

import java.util.*;

public class Solution {

    public static void main(String[] args) {
      Scanner stdin=new Scanner(System.in);
      for(int i = 0; i < 3; i++){
        int a=stdin.nextInt();
        System.out.println(a);
      }
   }
}

Approach 3.

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
         
        System.out.println(sc.nextInt());
        System.out.println(sc.nextInt());
        System.out.println(sc.nextInt());
    }
}

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×