Results 1 to 3 of 3
  1. #1
    sara2017 is offline Member
    Join Date
    Apr 2017
    Rep Power

    Default Please explain why

    why this working

    t = scan.nextLine();

    and this not working

    t = scan.nextLine();

    Java Code:
     package test;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    public class test {
        public static void main(String[] args) {
            int i = 4;
            double d = 4.0;
            String s = "HackerRank ";
            Scanner scan = new Scanner(;
            /* Declare second integer, double, and String variables. */
            int j=0;
            double o =0.0;
            String t ;
                    /* Read and save an integer, double, and String to your variables.*/
            System.out.print("Enter value of j :");
            System.out.print("Enter value of o :");
           System.out.print("Enter value of t :");
            t = 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. */
                System.out.println("Sum is " + (i + j));
                    /* Print the sum of the double variables on a new line. */
                System.out.println("Sum is " + (d + o));
                    /* Concatenate and print the String variables on a new line; 
                    	the 's' variable above should be printed first. */
                 System.out.println(s + t);

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: Please explain why

    Edit: forget my comment. Scanners are stupid.
    Last edited by SurfMan; 05-18-2017 at 02:02 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Please explain why

    This is to get around the fact that the other next...() methods leave the new line character(s) in the buffer, and nextLine() only reads up to (and including) a new line.
    Without the throwaway nextLine() all you are doing is reading an empty string.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. can someone explain what this exactly does?
    By liluma in forum New To Java
    Replies: 4
    Last Post: 08-21-2011, 07:58 PM
  2. Anyone can explain this?
    By kazumahits in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 02:03 AM
  3. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  4. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  5. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 06:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts