0

Here is my code:

public class Solution { public static void main(String[] args) { /* * Enter your code here. Read input from STDIN. * Print output to STDOUT. * Your class should be named Solution. */ int num = 0; double dou = 0.0; String s = null; Scanner in = new Scanner(System.in); if (in.hasNextInt()) { num = in.nextInt(); } Scanner d = new Scanner(System.in); if (d.hasNextDouble()) { dou = d.nextDouble(); } Scanner str = new Scanner(System.in); if (str.hasNextLine()) { s = str.nextLine(); } System.out.println("String:" + s); System.out.println("Double:" + dou); System.out.println("Int:" + num); } } 

I am getting this output:

String:null
Double:0.0
Int:42

But it should should look like this:

String: Welcome to Hackerrank Java tutorials!
Double: 3.1415
Int: 42

Can anyone explain me why I'm getting a null value for the string and 0.0 for the double?

2
  • why do you want 3 different scanners ? Commented Aug 11, 2015 at 7:15
  • what values you are giving?as per code, first you need to enter Int value, then enter, then Double value, enter, and then String value Commented Aug 11, 2015 at 7:25

10 Answers 10

2

//your answer

import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int i = in.nextInt(); double d = in.nextDouble(); in.nextLine(); // finishes the previous line String s = in.nextLine(); System.out.println("String: " + s); System.out.println("Double: " + d); System.out.println("Int: " + i); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Could you please explain, why do we need one extra in.nextLine(); here?
0

You should not use 3 scanners, one is enough. see Scanner method opened and closed twice.

Apart from that, when using only one, there can still be a problem like in this question: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

Comments

0

It works fine when i test it. How do you enter your float ? 3.1415 or 3,1415 ? Depending on your local one will be red and the other not.

And if want the result to be in one line:

 String chaine= String.format("String: %s. Double: %.5f. Int: %d", s,dou,num); System.out.println(chaine); 

6 Comments

i have fixed my float value error but still i am not able to get the whole string
if(in.hasNextLine()){ s = in.next(); And i m just getting the output String:Welcome; but expected output is String: Welcome to Hackerrank Java tutorials!
@GauravTiwari You don't need to change anything. The code you've provided works fine. Or you're hiding some crucial parts of your code which causes your problems. So please provide an example which reproduces your problem.
"One of them will throw an exception" did you test it ? It doesn't throw any exception.: 42 3.1415 sdfqdsf String: sdfqdsf. Double: 0,00000. Int: 42
Yes, you're right, he still has his hasNextXXX methods. But that still doesn't tell us why his String should be null. :(
|
0

This solution works

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

1 Comment

Maybe you could point the mistakes made instead of just posting corrected code
0
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 = scan.next(); System.out.println("String: " + s); System.out.println("Double: " + d); System.out.println("Int: " + i); } } 

Comments

0

You can do something like this. for every new line scanner.nextLine() should be called.

Scanner scan = new Scanner(System.in); int i = scan.nextInt(); String s=""; Double d=0d; if(scan.hasNextLine()){ scan.nextLine(); if(scan.hasNextDouble()){ d=scan.nextDouble(); scan.nextLine(); s=scan.nextLine(); }else{ s=scan.nextLine(); scan.nextLine(); d=scan.nextDouble(); } } 

Comments

0

Solution

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

1 Comment

Kindly explain what didn't work in the posted Question, and what/why you think your solution fixes the problem.
0

I fixed it with the following code:

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

Comments

-1

hey this error is because the java does not have fflush() in like C hence when you press enter after double or integer value the buffer contain enter key which is empty hence when you print using nextLine() it prints that line is empty hence error occurs so you have to call it to the next line using sc.nextLine() function hence the correct output will be displayed

Comments

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

1 Comment

we have to provide "scan.nextLine" after scan.nextDouble(); because the moment you press "enter key" after inserting double, then this "enter key" is treated as a null value and stored in String s

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.