<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aditya Sinha</title>
    <description>The latest articles on DEV Community by Aditya Sinha (@aditya_sinha_234).</description>
    <link>https://dev.to/aditya_sinha_234</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3754283%2F281db446-0283-4a25-96b7-fc49c90d0289.jpg</url>
      <title>DEV Community: Aditya Sinha</title>
      <link>https://dev.to/aditya_sinha_234</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aditya_sinha_234"/>
    <language>en</language>
    <item>
      <title>Understanding Java's Scanner: Why Mixing nextInt() and nextLine() Breaks Your Code</title>
      <dc:creator>Aditya Sinha</dc:creator>
      <pubDate>Fri, 27 Feb 2026 13:31:32 +0000</pubDate>
      <link>https://dev.to/aditya_sinha_234/why-nextline-reads-an-empty-string-after-nextint-in-java-13pd</link>
      <guid>https://dev.to/aditya_sinha_234/why-nextline-reads-an-empty-string-after-nextint-in-java-13pd</guid>
      <description>&lt;p&gt;Almost every java learner encounters this problem at some point.&lt;/p&gt;

&lt;p&gt;You write a simple program that asks for an age (an &lt;code&gt;int&lt;/code&gt;) and then a name (a &lt;code&gt;String&lt;/code&gt;). You run it, type an age, press &lt;strong&gt;Enter&lt;/strong&gt;, and before you've typed a single letter of the name, the program has already finished running.&lt;/p&gt;

&lt;p&gt;It feels like a glitch.&lt;/p&gt;

&lt;p&gt;But, in reality, you have encountered what developers call the &lt;strong&gt;Scanner buffer issue&lt;/strong&gt;. To keep things simple for us beginners, I like to call it &lt;strong&gt;the nextLine() issue&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problem
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Code That “Should” Work&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Take a look at this snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an age"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Example input: 25&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter a name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Example input: Ahi&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Age: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might expect the following interaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enter an age
25
Enter a name
Ahi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sadly, &lt;strong&gt;it is not that simple.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you type &lt;code&gt;25&lt;/code&gt; and press Enter. The program finishes before you can even type the name. Therefore, the actual output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Name:
Age: 25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why did this happen? Let’s investigate.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important note:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve included a quick &lt;strong&gt;glossary&lt;/strong&gt; at the bottom to help you with unfamiliar terms.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Investigation
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;What Happens Inside?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you use &lt;code&gt;Scanner&lt;/code&gt; class to read input from the keyboard, every key you press is first stored inside the &lt;strong&gt;input buffer&lt;/strong&gt; as a character.&lt;/p&gt;

&lt;p&gt;The characters remain in the buffer until:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The program consumes them (that is, takes them out of the buffer) using methods like &lt;code&gt;next()&lt;/code&gt;, &lt;code&gt;nextInt()&lt;/code&gt;, &lt;code&gt;nextLine()&lt;/code&gt;, or&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The program terminates.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;Scanner&lt;/code&gt; class reads input using two strategies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Token-based parsing&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Line-based parsing&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every character inside the buffer is examined one by one.&lt;br&gt;&lt;br&gt;
Depending on the parsing rules being applied, a character will either be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consumed and returned&lt;/strong&gt; (removed from the buffer and included in the result)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consumed and discarded&lt;/strong&gt; (removed from the buffer but &lt;strong&gt;not&lt;/strong&gt; returned as part of the result)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Left in the buffer&lt;/strong&gt; (to be processed later)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy8ldjru22dkuxuossixa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy8ldjru22dkuxuossixa.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Token-Based Parsing
&lt;/h3&gt;

&lt;p&gt;In this approach, input is divided into &lt;strong&gt;tokens&lt;/strong&gt; and one token is returned at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rules:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Leading &lt;strong&gt;delimiters&lt;/strong&gt; are consumed and discarded.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From the first non-delimiter character, characters are consumed until a delimiter is detected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The detected delimiter is left in the buffer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The consumed characters are returned to the program as one token.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Methods that use this approach:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;next()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nextInt()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nextDouble()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nextFloat()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nextLong()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nextBoolean()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fin3mqwa9psnxijom0ujq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fin3mqwa9psnxijom0ujq.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Line-Based Parsing
&lt;/h3&gt;

&lt;p&gt;In this approach, input is read as an &lt;strong&gt;entire line&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rules:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Characters are consumed continuously, including whitespace characters (spaces and tabs).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reading continues until a line terminator (&lt;code&gt;'\n'&lt;/code&gt;) is encountered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The line terminator (&lt;code&gt;'\n'&lt;/code&gt;) is consumed and discarded.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All characters before the line terminator are returned as a single &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Method that uses this approach:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nextLine()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fax410jb2ae9q0eoxz3dv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fax410jb2ae9q0eoxz3dv.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h1&gt;
  
  
  Observation
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Understanding the Behaviour&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we understand how input is read, let us trace the execution of our “failing” code to identify where things went wrong.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Input Entry&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When the program runs, the terminal displays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Enter an age
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You type &lt;code&gt;25&lt;/code&gt; and press &lt;strong&gt;Enter&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer Status:&lt;/strong&gt; &lt;code&gt;['2'] ['5'] ['\n']&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;code&gt;nextInt()&lt;/code&gt; &lt;strong&gt;Executes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following the rules of token-based parsing, &lt;code&gt;nextInt()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Begins reading from the buffer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consumes &lt;code&gt;'2'&lt;/code&gt; and &lt;code&gt;'5'&lt;/code&gt; (non-delimiter characters)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encounters &lt;code&gt;'\n'&lt;/code&gt;, which is a delimiter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stops reading but does &lt;strong&gt;not&lt;/strong&gt; consume the delimiter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parses the token &lt;code&gt;"25"&lt;/code&gt; into the integer value &lt;code&gt;25&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Returns &lt;code&gt;25&lt;/code&gt; to the program&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer Status:&lt;/strong&gt; &lt;code&gt;['\n']&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The line feed remains in the buffer.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 3: Prompt Appears&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The terminal now shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Enter&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
&lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="nc"&gt;Enter&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Misconception: You might expect the program to pause here and wait for you to type &lt;code&gt;Ahi&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Reality: The program pauses only if the buffer is &lt;strong&gt;empty&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;code&gt;nextLine()&lt;/code&gt; &lt;strong&gt;Executes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following the rules of line-based parsing, &lt;code&gt;nextLine()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Checks the buffer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Immediately detects &lt;code&gt;'\n'&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consumes and discards the line feed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Returns everything before &lt;code&gt;'\n'&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There were no characters before it.&lt;/p&gt;

&lt;p&gt;Therefore, it returns an empty string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Step 5: Final Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The program executes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Age: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name:
Age: 25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;Now you understand where the problem occurred and why. Let us fix it.&lt;/p&gt;

&lt;p&gt;There are several ways to solve this; we will discuss a few.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Quick Fix
&lt;/h3&gt;

&lt;p&gt;The most straightforward solution is to consume the leftover line feed.&lt;/p&gt;

&lt;p&gt;Add an extra &lt;code&gt;nextLine()&lt;/code&gt; immediately after &lt;code&gt;nextInt()&lt;/code&gt; to clear the buffer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an age"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 

&lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Consumes and discards the leftover '\n'&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter a name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why it Works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nextInt()&lt;/code&gt; leaves the line feed character (&lt;code&gt;'\n'&lt;/code&gt;) in the input buffer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The additional &lt;code&gt;nextLine()&lt;/code&gt; consumes that &lt;code&gt;'\n'&lt;/code&gt; and removes it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, when the second &lt;code&gt;nextLine()&lt;/code&gt; executes, the buffer is clean, so the program waits for your name input.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. The Professional Approach
&lt;/h3&gt;

&lt;p&gt;Avoid mixing token-based and line-based parsing.&lt;/p&gt;

&lt;p&gt;Instead, read all input using &lt;code&gt;nextLine()&lt;/code&gt; and convert numeric values explicitly using parsing methods such as &lt;code&gt;Integer.parseInt()&lt;/code&gt;. This method takes a numeric string (for example, &lt;code&gt;"25"&lt;/code&gt;) and converts it into a primitive &lt;code&gt;int&lt;/code&gt; value (&lt;code&gt;25&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an age:"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt; 

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter a name:"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why it Works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nextLine()&lt;/code&gt; consumes the line feed character each time it reads input, so no delimiters remain in the buffer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parsing explicitly using methods such as &lt;code&gt;Integer.parseInt()&lt;/code&gt; gives you greater control over input handling.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Worth knowing:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;if the input isn't a valid number, &lt;code&gt;Integer.parseInt()&lt;/code&gt; throws a &lt;code&gt;NumberFormatException&lt;/code&gt;. In a real application, you would typically validate the input before parsing it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other Parsing Methods:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The same pattern applies to other numeric types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Double.parseDouble()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Float.parseFloat()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Long.parseLong()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Boolean.parseBoolean()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. The Duct Tape Fix
&lt;/h3&gt;

&lt;p&gt;You can reverse the order of the inputs and avoid the issue.&lt;br&gt;&lt;br&gt;
If you read the &lt;code&gt;String&lt;/code&gt; first and the &lt;code&gt;int&lt;/code&gt; afterwards, the problem seems as though it never occurred.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter a name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an age"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This is Wrong:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;nextInt()&lt;/code&gt; still leaves &lt;code&gt;'\n'&lt;/code&gt; in the input buffer.&lt;/p&gt;

&lt;p&gt;If the program later requires another call to &lt;code&gt;nextLine()&lt;/code&gt;, the same issue will immediately reappear.&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Now you understand the whys, the hows, the dos and the don’ts.&lt;/p&gt;

&lt;p&gt;In Java, nothing is broken. The behaviour is defined, the rules are strict, and the machine is consistent. Use that consistency to your advantage rather than fighting against it.&lt;/p&gt;

&lt;p&gt;Try the following example yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter your full name:"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enter your own name, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Aditya Sinha
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Aditya Sinha
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pause and think.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Why does this work?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How does &lt;code&gt;next()&lt;/code&gt; decide where to stop reading?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What remains in the input buffer afterwards?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What happens if the name has more than two parts?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Glossary
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Short and Simple&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Buffer
&lt;/h3&gt;

&lt;p&gt;When data is being transferred from a source (Point A) to a destination (Point B), it is temporarily stored in a small memory area between them called a &lt;strong&gt;buffer&lt;/strong&gt;. It serves as a waiting room for data and typically follows the &lt;strong&gt;FIFO&lt;/strong&gt; (First In, First Out) principle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input Buffer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Source: Keyboard&lt;br&gt;&lt;br&gt;
Destination: Java program&lt;/p&gt;

&lt;p&gt;However, internally, the data passes through multiple layers before reaching the Java program.&lt;/p&gt;


&lt;h3&gt;
  
  
  Token
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;token&lt;/strong&gt; is a continuous sequence of non-delimiter characters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Token formation&lt;/strong&gt; is the internal act of grouping characters together until a delimiter is found.&lt;/p&gt;

&lt;p&gt;Input buffer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="sc"&gt;'2'&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="sc"&gt;'5'&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="sc"&gt;'h'&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="sc"&gt;'i'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tokens formed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"25"&lt;/span&gt;
&lt;span class="s"&gt;"Ahi"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Parsing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Theoretical view.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you type &lt;code&gt;25&lt;/code&gt; through the keyboard, the computer receives (represented internally in binary form):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="sc"&gt;'2'&lt;/span&gt;  &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;ASCII&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;  
&lt;span class="sc"&gt;'5'&lt;/span&gt;  &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;ASCII&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;53&lt;/span&gt;
&lt;span class="sc"&gt;'\n'&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;ASCII&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this stage, the computer does not recognise the integer &lt;code&gt;25&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
It only sees the sequence of characters &lt;code&gt;'2'&lt;/code&gt;, &lt;code&gt;'5'&lt;/code&gt;, and &lt;code&gt;'\n'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Inside a &lt;code&gt;nextInt()&lt;/code&gt; method of the &lt;code&gt;Scanner&lt;/code&gt; class:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Token Formation occurs:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="sc"&gt;'2'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;'5'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"25"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A String token &lt;code&gt;"25"&lt;/code&gt; is formed.&lt;br&gt;&lt;br&gt;
At this point, it is still text, not a numeric value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Actual Parsing:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Parsing now occurs inside &lt;code&gt;nextInt()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The String &lt;code&gt;"25"&lt;/code&gt; is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Checked to ensure it is a valid integer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Converted into the numeric value &lt;code&gt;25&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"25"&lt;/span&gt;  &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="nf"&gt;parsing&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;25&lt;/span&gt;    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="nf"&gt;parsing&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The integer value is then returned to the program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parsing&lt;/strong&gt; here simply means converting characters into a meaningful data type.&lt;br&gt;&lt;br&gt;
This is different from compiler parsing, which involves analysing the grammatical structure of an entire program.&lt;/p&gt;

&lt;p&gt;In both cases, however, the core idea remains the same: Interpreting input according to defined rules.&lt;/p&gt;


&lt;h3&gt;
  
  
  Whitespace Character
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;whitespace character&lt;/strong&gt; has no visible symbol. It appears as blank space and is used to create horizontal or vertical separation between visible characters.&lt;/p&gt;

&lt;p&gt;A few examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;' '&lt;/code&gt; (space)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'\t'&lt;/code&gt; (tab)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'\n'&lt;/code&gt; (line feed)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;System.out.println&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Name"&lt;/span&gt; + &lt;span class="s2"&gt;" "&lt;/span&gt; + &lt;span class="s2"&gt;"Age"&lt;/span&gt; + &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; + &lt;span class="s2"&gt;"Ahi"&lt;/span&gt; + &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; + &lt;span class="s2"&gt;"25"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Name Age
Ahi     25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Escape Sequence Character
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;escape sequence&lt;/strong&gt; is a special character representation used inside a string. It always starts with a &lt;strong&gt;backslash&lt;/strong&gt; (&lt;code&gt;\&lt;/code&gt;), which acts as a signal to the compiler to "escape" its normal rules of treating a character.&lt;/p&gt;

&lt;p&gt;These characters cannot be typed directly inside a string because it has special meaning in Java.&lt;/p&gt;

&lt;p&gt;For example, double quotes &lt;code&gt;"&lt;/code&gt; are used to mark the beginning and end of a string.&lt;br&gt;&lt;br&gt;
If you try to use them directly inside a string, the compiler gets confused.&lt;/p&gt;

&lt;p&gt;A few escape sequence characters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\n&lt;/code&gt; (Line Feed)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\t&lt;/code&gt; (Tab)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\"&lt;/code&gt; (Double Quote)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to print text inside double quotes, you have to use escape sequence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"He said, \"Hello!\""&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without &lt;code&gt;\"&lt;/code&gt;, Java would think the string ends before &lt;code&gt;Hello!&lt;/code&gt;, causing a compilation error.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;He said, "Hello!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Escape sequences and whitespace characters are not the same thing.&lt;/p&gt;

&lt;p&gt;While whitespace is any character that creates a blank space, escape sequence characters are not always blank spaces.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Escape sequence&lt;br&gt;&lt;br&gt;
Not whitespace&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\n&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Escape sequence&lt;br&gt;&lt;br&gt;
Also whitespace&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;' '&lt;/code&gt; (space)&lt;br&gt;&lt;br&gt;
Whitespace&lt;br&gt;&lt;br&gt;
Not an escape sequence&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Delimiter
&lt;/h3&gt;

&lt;p&gt;In the context of &lt;code&gt;Scanner&lt;/code&gt; and input processing in Java, a &lt;strong&gt;delimiter&lt;/strong&gt; is a character or pattern used to separate input into smaller pieces called tokens. It marks where one token ends and the next begins like a boundary.&lt;/p&gt;

&lt;p&gt;By default, &lt;code&gt;Scanner&lt;/code&gt; uses whitespace characters as delimiters.&lt;/p&gt;

&lt;p&gt;When you type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;25 Ahi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The space acts as a delimiter, and &lt;code&gt;Scanner&lt;/code&gt; sees two tokens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;25] &lt;span class="o"&gt;[&lt;/span&gt;Ahi]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even change it using &lt;code&gt;useDelimiter()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sc.useDelimiter&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;","&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the comma becomes the delimiter.&lt;br&gt;&lt;br&gt;
If the input is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;25,Ahi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tokens will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;25] &lt;span class="o"&gt;[&lt;/span&gt;Ahi]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Line terminator
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;line terminator&lt;/strong&gt; is a kind of whitespace characters (also an escape sequence character) that is used to move the cursor to a new line.&lt;/p&gt;

&lt;p&gt;Few examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;‘\n’&lt;/code&gt; (Line feed)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'\r'&lt;/code&gt; (Carriage return)&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello\nWorld"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello
World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Note
&lt;/h1&gt;

&lt;p&gt;Everything above is a great mental model for understanding &lt;em&gt;why&lt;/em&gt; this bug happens, but it's still a simplified picture. Under the hood, &lt;code&gt;Scanner&lt;/code&gt; doesn't actually go character by character. It uses &lt;strong&gt;regex pattern matching&lt;/strong&gt; to find tokens in the buffer. If you're curious to see the real thing, here's where to look:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html" rel="noopener noreferrer"&gt;&lt;code&gt;Scanner&lt;/code&gt; class, Java SE Documentation&lt;/a&gt;: the official reference for how &lt;code&gt;nextInt()&lt;/code&gt;, &lt;code&gt;nextLine()&lt;/code&gt;, and delimiters really work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Scanner.java" rel="noopener noreferrer"&gt;OpenJDK source for &lt;code&gt;Scanner.java&lt;/code&gt;&lt;/a&gt;: the actual tokenizing code, if you want to trace it yourself.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Let's Connect!
&lt;/h1&gt;

&lt;p&gt;If you found this helpful, or have any doubts or queries, feel free to reach out via the links below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.linkedin.com/in/adityasinha234/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://x.com/AdityaSinha234" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>indepth</category>
    </item>
  </channel>
</rss>
