I am getting a weird runtime error while trying to input a String
.
I input the String
using Scanner
Class by using the nextLine()
function :
System.out.println("Enter a String.");
String str = in.nextLine();
I put random printing messages in between the lines of my code to see why this is happening and to know the origin of error. After eliminating every possible source of error, I have finally come to the conclusion that the program automatically accepts an empty String.
I looked up the program I was working on the internet and what i found was that when I write this, :
System.out.println("Enter a String.");
in.nextLine();
String str = in.nextLine();
everything seems to work properly. The program inputs the String
properly without any problem.
Please note that I am just a beginner in Java. In my (very little) experience, nextLine()
function never gave any such error. Never. It always worked properly. But this time, I am very confused. Please help me out.
EDIT : Also, my program compiled successfullly without any compile errors.