Wednesday, February 13, 2013

Difference between next() and nextLine() methods in 
Scanner class of Java

If we look into the Java specification for both of these methods then it is similar. Both of them are returning String object; Are the java implementers are crazy? Isn’t it? No man it is nothing like this. However, both of these two method are returning strings, they has minor difference.

·                    public String next()
·                   public String nextLine()

Let’s don’t waste time on making story and do some experiment.

Program # 1
import java.util.Scanner;

public class TestClass {

       /**
        * Try to clear doubt for next() and nextLine() methods in Scanner class
        *
        * @param args
        * @author Bhajan Mehta
        */
       public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              System.out.println("Enter First String : ");
              String str = scanner.nextLine();
              System.out.println("String 1 : " + str);
              System.out.println("Enter 2nd String : ");
              String str2 = scanner.nextLine();
              System.out.println("String 2 : " + str2);

       }

}

Output:
1      Enter First String :
2      Bhajan
3      String 1 : Bhajan
4      Enter 2nd String :
5      Mehta
6      String 2 : Mehta

Try to run the above program, let me know if you are getting expected output or not?
Obliviously, you should get expected output (If your expectations are aligned with the Java Complier).

Let me try to do a small change in the above program by replacing first nextLine() method with next().

Now, Before executing the below program, just guess how its output should be different from the above one.

If you have guessed than let’s go ahead.

Compile and execute again this program.

Program # 2
import java.util.Scanner;

public class TestClass {

       /**
        * Try to clear doubt for next() and nextLine() methods in Scanner class
        *
        * @param args
        * @author Bhajan Mehta
        */
       public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              System.out.println("Enter First String : ");
              String str = scanner.next();
              System.out.println("String 1 : " + str);
              System.out.println("Enter 2nd String : ");
              String str2 = scanner.nextLine();
              System.out.println("String 2 : " + str2);

       }

}

Here is the output:
1      Enter First String :
2      Bhajan
3      String 1 : Bhajan
4      Enter 2nd String :
5      String 2 :
6

What happen? It didn’t ask you to enter 2nd string? How the crap this is. Isn’t it?

It is showing different output:
1   1.     2nd program is not asking for the 2nd input.
    2 .   Cursor is remaining in the same line (last printed line of output) in the program # 1, however cursor is coming on the next line to the last printed line in the output in program #2.

But, there is nothing wrong. It is working as it has indent to do.

I hope you have now understood the difference.

nextLine() method returns only string excluding any line separator at the end of entered string. But next() method gets only string and treating “\n” (Enter) as the second input.


No comments:

Post a Comment