0
public class Solution {

    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";
        Scanner scan = new Scanner(System.in);

        /* Read and save an integer, double, and String to your variables.*/
        int j=scan.nextInt();
        double p=scan.nextDouble(); 
        String n=scan.nextLine();
        // Note: If you have trouble reading the entire String, please go back and 
        // review the Tutorial closely.

        /* Print the sum of both integer variables on a new line. */
        int k=i+j;
        System.out.println(""+k);

        /* Print the sum of the double variables on a new line. */
        double o=p+d;
        System.out.println(""+o);

        /* Concatenate and print the String variables on a new line; 
           the 's' variable above should be printed first. */
        String s3=s.concat(n);
        System.out.println(s3);
        scan.close();
    }
}

I am getting the problem in string in input in the given code though sccaner not printing the entire line.I tried using nextLine() but still it not working as i have to print entire string.

- Input (stdin)
   12
   4.0 
   is the best place to learn and practice coding! 
   Your Output (stdout) 
   16
   8.0
   HackerRank   
   Expected Output 
   16
   8.0 
   HackerRank is the best place to learn and practice coding!
tester
  • 5
  • 4
mridul
  • 21
  • 1
  • 4
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Tom Jun 20 '17 at 08:01

1 Answers1

0

The documentation of nextLine (<== link you should read) says:

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

In above case, it returns the rest of the line after "4.0" which is the empty string.

Apparently the input tokens are delimited by newlines so add the second line bellow after creating the Scanner:

// ...
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\\n");
// ...

The pattern "\\n" means a backslash followed by an 'n', which is interpreted as a newline character, see Pattern.

(workaround/hack: instead of useDelimiter, just ignore the rest of the line and use a second nextLine() - not that nifty)

tester
  • 5
  • 4