Java: String after Integer causing error?

Error while reading a string after an integer

It's been a while since I've started programming with Java, although I felt really uncomfortable in the very beginning, it feels better now. Java does include more characters to type than other programming languages but it is a very important language also known as portable language. Java is fast, secure, reliable and one of the most respected language in the world of developers. It is widely used for developing Java applications in laptops, game consoles, scientific supercomputers, projects with android and big data, etc.

Now, let's back to the discussion of the error which is caused when a string input is taken after integer input. We will discuss about eliminating this error and knowing why this error occurs. For better understanding, lets take a question:

Write a program that prints month and year from the user input.

import java.util.Scanner;

public class HashBlog {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter year: ");
        int year = in.nextInt();      // integer input command
        System.out.println("Enter month: ");
        String month = in.nextLine(); // string input command
        System.out.println("Displaying the entered is "+month+", "+year);
    }
}

The Scanner class helps in creating as many Scanner objects we require. When a new Scanner object is created, System.in is passed as a parameter to its constructor. Once we have created the Scanner object we can use the Scanner method, nextLine() , to prompt the user for input in the terminal. The scanner class allows to receive input from the terminal. When receiving input, in most languages, the default data type of input from the terminal is a string and if an integer or float was required then, you will have to cast the input to the required data type. Note that println() is used here but why? print() enters the input on the same line with no new line. So, to print the input in a new line println() is used as standard convention. Now, let's run the code:

blog1output.png Oops! So, here's the mentioned error! Now let's take a closer look and resolve it. Did you see that the code looks perfectly fine but there's one thing that is not usually discussed much: string type input after integer type input. Always remember that making errors aren't bad. We are always learning more through these errors:

It is the errors that make any subject insightful.

If you try the above code, you will notice that right after entering the year, the program exits without string input. Thus, leaving the string input empty and executing the complete code. This is because of the new line character during input. The nextInt() function, does not read the new line character of your input. So, when you command nextLine() it will consider the new line input as a part of string character. That’s why the nextLine() command after the nextInt() command gets skipped.

How do you overcome this?

You can easily overcome this error with another nextLine() command between the integer and string input command as shown below:

import java.util.Scanner;

public class HashBlog {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter year: ");
        int year = in.nextInt();      // integer input command
        in.nextLine();                //this helps in eliminating the error
        System.out.println("Enter month: ");
        String month = in.nextLine(); // string input command
        System.out.println("Displaying the entered is "+month+", "+year);
    }
}

and Tadaa!! The code got executed without skipping any lines.

foutputblog1.png

Conclusion

If you're looking for any other way to eliminate this issue, then you can read the strings before integer or floats,etc. I feel, this solution easier as you'll have to simply call 'nextLine()' twice. Once, to remove the newline character in the terminal, and a second time to read the string. I hope I was able to explain the issue and the solution.

Thank you for reading.