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?

Tom
  • 16,842
  • 17
  • 45
  • 54
Gaurav Tiwari
  • 111
  • 2
  • 2
  • 10

9 Answers9

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);
    }
}
benjaminP
  • 3
  • 3
bhalla ram
  • 29
  • 2
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);
Erwan C.
  • 709
  • 5
  • 18
  • i have fixed my float value error but still i am not able to get the whole string – Gaurav Tiwari Aug 11 '15 at 07:36
  • 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! – Gaurav Tiwari Aug 11 '15 at 07:37
  • @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. – Tom Aug 11 '15 at 07:43
  • 1
    "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 – Erwan C. Aug 11 '15 at 07:46
  • Yes, you're right, he still has his `hasNextXXX` methods. But that still doesn't tell us why his String should be `null`. :( – Tom Aug 11 '15 at 07:56
  • In his code, there is no reason for the string to be null – Erwan C. Aug 11 '15 at 08:04
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?

Community
  • 1
  • 1
user140547
  • 7,750
  • 3
  • 28
  • 80
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();

}
SDangat
  • 9
  • 2
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);
    }
}
kara
  • 3,205
  • 4
  • 20
  • 34
Vid
  • 5
  • 2
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();
            }

        }
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);
        }
  • Kindly explain what didn't work in the posted Question, and what/why you think your solution fixes the problem. – Scratte Apr 02 '20 at 18:41
-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

sujit amin
  • 121
  • 3
  • 10
-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);

}
  • 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 – jagadish Beejapu Jun 04 '18 at 09:18