Java Stdin and Stdout II in Java:

Java Stdin and Stdout II in Java:

Hello Friends, How are you? I hope you are all fine and solving HackerRank’s Java problems. In this Series of Solutions to HackerRank’s Java Problems today I am going to solve the HackerRank Java Stdin and Stdout II Problem with a very easy explanation. This is the 4th problem of Java on HackerRank. In this article, you will get more than one approach to solve this problem. First of all, we’ll discuss the problems after that we’ll solve them. So let’s start- 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. To make the problem a little easier, a portion of the code is provided for you in the editor. There are three lines of input:

  1. The first line contains an integer.
  2. The second line contains a double.
  3. The third line contains a String.

There are three lines of output:

  1. On the first line, print String: followed by the unaltered String read from stdin.
  2. On the second line, print Double: followed by the unaltered double read from stdin.
  3. On the third line, print Int: followed by the unaltered integer read from stdin.

To make the problem easier, a portion of the code is already provided in the editor. Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).

42 3.1415 Welcome to HackerRank’s Java tutorials! (code-box)

Sample Output:

String: Welcome to HackerRank’s Java tutorials! Double: 3.1415 Int: 42 (code-box)

Editorial of Java Stdin and Stdout II

When switching between reading tokens of input and reading a full line of input, you need to make another call to nextLine() because the Scanner object will read the rest of the line where its previous read left off; if there is nothing on the line, it simply consumes the newline and moves to the beginning of the next line. In the code below, the nextDouble() method stops reading at the end of the second line of input, but does not move the Scanner object to the next (third) line. Because of this, the subsequent call to nextLine() reads the rest of the now-empty second line, consuming the newline and moving the Scanner to the beginning of the third line. Once the Scanner object is at the beginning of the third line, we can call nextLine() again and successfully read the line’s contents.

Java Stdin and Stdout II – HackerRank Solution

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

Approach II:

import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); double y=sc.nextDouble(); sc.nextLine(); String s=sc.nextLine(); System.out.println("String: "+s); System.out.println("Double: "+y); System.out.println("Int: "+x); }
}

Approach III:

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 = scan.nextLine(); if(scan.hasNextLine() || s.isEmpty()){ 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. If you have any problem regarding this post or website fill the contact form.

No Comments

Sorry, the comment form is closed at this time.