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

    Default Please explain why

    why this working

    scan.nextLine();
    t = scan.nextLine();

    and this not working

    t = scan.nextLine();


    Java Code:
     package test;
    import java.io.*;
    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(System.in);
            /* 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 :");
            j=scan.nextInt();
            System.out.print("Enter value of o :");
            o=scan.nextDouble();
           System.out.print("Enter value of t :");
           scan.nextLine();
            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
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: Please explain why

    Edit: forget my comment. Scanners are stupid.
    Last edited by SurfMan; 05-18-2017 at 03: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
    Posts
    13,532
    Rep Power
    24

    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, 08:58 PM
  2. Anyone can explain this?
    By kazumahits in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 03:03 AM
  3. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  4. Replies: 3
    Last Post: 12-13-2010, 08:22 AM
  5. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 07: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
  •