Java Stdin and Stdout I - HackerRank Solution

Java Stdin and Stdout I - HackerRank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java Stdin and Stdout I Problem with a very easy explanation. This is the 2nd problem of Java on HackerRank. In this article, you will get more than three approaches to solve this problem. So let's start-

Java Stdin and Stdout I - Hacker Rank Solution


Table of Content (toc)

HackerRank Java Stdin and Stdout I - Problem Statement

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); (code-box)

The code above creates a Scanner object named Scanner 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 (code-box)

Our code will print:

myString is: Hi myInt is: 5 (code-box)

Alternatively, you can use the BufferedReader class.

Task

In this challenge, you must read 3 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 3 lines of input, and each line contains a single integer.

Sample Input

42 100 125 (code-box)

Sample Output

42 100 125 (code-box)

Java Stdin and Stdout I - HackerRank Solutions

Approach I:

import java.util.*;

public class Solution  
{

    public static void main(String[] args)  
   {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
       
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}


Approach II:

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 III:

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 IV:

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());
    }
}


Disclaimer: The above Problem ( Java Stdin and Stdout I ) is generated by Hackerrank but the Solution is Provided by Code Solution. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution of the Java Stdin and Stdout I HackerRank Problem. All these four solutions will pass all the test cases. Now visit Java Stdin and Stdout I HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.