Java Stdin and Stdout II in Java: HackerRank Solution

Java Stdin and Stdout II in Java: Hacker Rank Solution

Hello Friends, How are you? Today I am going to share Java Stdin and Stdout II HackerRank Problem. This is a very important problem. And in this article, I am going to solve this problem with a very easy explanation. The solution to the problem given in this article will pass all the test cases. If you want to understand the solution then please read the complete article or if you want only the solution then please jump directly to the code. So let's start -

Java Stdin and Stdout II in Java – Hacker Rank Solution


Table of Content (toc)

HackerRank Java Stdin and Stdout II - Problem Statement

In this challenge, you must read an integer, a double, and a String from stdin, then print the values according to the instructions in the Output Format section below. A portion of the code is provided for you in the editor to make the problem a little easier.

Note: We recommend completing Java Stdin and Stdout I before attempting this challenge.



Standard Input Method in Java - Java Stdin

The standard input(stdin) can be represented by System.in in Java. The System.in is an instance of the InputStream class. It means that all its methods work on bytes, not Strings. To read any data from a keyboard, we can use either a Reader class or a Scanner class.

This class is used in the System class to define an attribute (a field) called in to represent the 'standard' input stream. Note that a common abbreviation for standard input is 'stdin'.

The Scanner class (package java.util.Scanner) is a text scanner that breaks up the input into tokens based on a delimiter pattern such as space (default), semi-colon, tab, etc. These tokens can then be converted to a specific data type.

Standard Output Method in Java - Java Stdout

The standard output in Java is the PrintStream class accessed through the System.out field. By default, traditional output prints to the display, also called the console.

PrintStream allows the printing of different data types converting all data to bytes. Two of its most useful methods include print and println methods. Both methods support multiple data types such as integers, characters, character arrays, strings, floats, objects, etc.

The print and println methods convert the data to bytes and print to the display. The only difference is that println also terminates the line, which means that the cursor goes to the next line.

Java Stdin and Stdout II - HackerRank Solution

Approach I:

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        double d = scan.nextDouble();
        String s="";
        while(scan.hasNext())
        {
            s=scan.nextLine();
        }
      
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}




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

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Java Stdin and Stdout II 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.