thestudentnetwork

Saturday 17 September 2016

Hackerrank Soluntions: Java Stdin and Stdout II



Task:
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.

Input format:
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.
Output format:
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.
Sample Input:
43
3.14
Welcome to the network.

Sample Output:
String: Welcome to the network.
Double: 3.14
Int: 43

Quick Bits:
  • Stdin: Standard Input
  • Stdout: Standard Output
  • Imports: java.util.*; (or) java.util.Scanner;

Program:
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 j=scan.nextLine();
        String s=scan.nextLine();

        // Write your code here.

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}
Share:

13 comments:

  1. why have you used extra string variable j?

    ReplyDelete
    Replies
    1. To print the double value

      Delete
    2. You should add "scan.nextLine" . and this is for removing buffer and making to display proper string.

      Delete
  2. Replies
    1. What buffer? explain in detail

      Delete
  3. why you have added extra string j

    ReplyDelete
  4. I wasted my entire day for this question

    ReplyDelete
  5. why did u add string j ?

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. 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();
    scan.nextLine();
    String s = scan.nextLine();

    // Write your code here.

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
    }
    }

    ReplyDelete
  8. please can anybody explain why have you used extra string variable j?

    ReplyDelete

Definition List

Contact

Pages

Support