Java Stdin and Stdout II | Hackerrank Problem Solutions
In this challenge, you want to read an integer, a double, and a String from stdin, then print the values consistent with the instructions within the Output Format section below. to form the matter a touch easier, some of the code is provided for you within the editor.
Input Format
There are three lines of input:- The first line contains an integer.
- The second line contains a double.
- The third line contains a String.
Output Format
There are three lines of output:- On the first line, print String: followed by the unaltered String read from stdin.
- On the second line, print Double: followed by the unaltered double read from stdin.
- On the third line, print Int: followed by the unaltered integer read from stdin.
To convert the problem more easier, some part of the code is already coded 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).
Sample Input
42
3.1415
Welcome to HackerRank's Java tutorials!
Sample Output
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
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 dup = scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d=scan.nextDouble();
String dup = scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
thanks for helping
ReplyDelete