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:
- The first line contains an integer.
- The second line contains a double.
- The third line contains a String.
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.
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); } }
why have you used extra string variable j?
ReplyDeletei have same question
DeleteTo print the double value
DeleteYou should add "scan.nextLine" . and this is for removing buffer and making to display proper string.
Deleteto clear the buffer.
ReplyDeleteWhat buffer? explain in detail
Deletewhy you have added extra string j
ReplyDeleteI wasted my entire day for this question
ReplyDeletewhy did u add string j ?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteimport java.util.Scanner;
ReplyDeletepublic 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);
}
}
Why are error in the code
Deleteplease can anybody explain why have you used extra string variable j?
ReplyDelete