<?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.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>Why nextLine() Reads an Empty String After nextInt() in Java?</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 (&lt;code&gt;int&lt;/code&gt;) and then a name (&lt;code&gt;String&lt;/code&gt;). You run it, type an age, press Enter, and the program appears to skip the name input entirely.&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 the &lt;strong&gt;&lt;code&gt;nextLine()&lt;/code&gt; issue&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code That “Should” Work
&lt;/h3&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, it is not that simple.&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 plaintext"&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;Important note:&lt;br&gt;
I’ve included a quick glossary at the bottom to help you with unfamiliar terms.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Investigation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Happens Inside?
&lt;/h3&gt;

&lt;p&gt;When you use the &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;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;/li&gt;
&lt;li&gt;The program terminates.&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;Token-based parsing&lt;/li&gt;
&lt;li&gt;Line-based parsing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every character inside the buffer is examined one by one.&lt;/p&gt;

&lt;p&gt;Depending on the parsing rules being applied, a character will either be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consumed and returned (removed from the buffer and included in the result)&lt;/li&gt;
&lt;li&gt;Consumed and discarded (removed from the buffer but not returned as part of the result)&lt;/li&gt;
&lt;li&gt;Left in the buffer (to be processed later)&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.amazonaws.com%2Fuploads%2Farticles%2Fbd35dbl9odit2s1vt1bn.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.amazonaws.com%2Fuploads%2Farticles%2Fbd35dbl9odit2s1vt1bn.png" alt=" "&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 tokens and one token is returned at a time.&lt;/p&gt;

&lt;h4&gt;
  
  
  Rules
&lt;/h4&gt;

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

&lt;h4&gt;
  
  
  Methods That Use This Approach
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;next()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nextInt()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nextDouble()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nextFloat()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nextLong()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nextBoolean()&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.amazonaws.com%2Fuploads%2Farticles%2F9ncn4vmso10lzwlt10p9.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.amazonaws.com%2Fuploads%2Farticles%2F9ncn4vmso10lzwlt10p9.png" alt=" "&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 entire line.&lt;/p&gt;

&lt;h4&gt;
  
  
  Rules
&lt;/h4&gt;

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

&lt;h4&gt;
  
  
  Method That Uses This Approach
&lt;/h4&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.amazonaws.com%2Fuploads%2Farticles%2F77obc1sdi1tl2dxixz6u.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.amazonaws.com%2Fuploads%2Farticles%2F77obc1sdi1tl2dxixz6u.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Observation: Understanding the Behaviour
&lt;/h2&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;h4&gt;
  
  
  Step 1: Input Entry
&lt;/h4&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 plaintext"&gt;&lt;code&gt;Enter an age
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You type:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Buffer status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;5&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 2: &lt;code&gt;nextInt()&lt;/code&gt; Executes
&lt;/h4&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;Begins reading from the buffer&lt;/li&gt;
&lt;li&gt;Consumes &lt;code&gt;'2'&lt;/code&gt; and &lt;code&gt;'5'&lt;/code&gt; (non-delimiter characters)&lt;/li&gt;
&lt;li&gt;Encounters &lt;code&gt;'\n'&lt;/code&gt;, which is a delimiter&lt;/li&gt;
&lt;li&gt;Stops reading but does not consume the delimiter&lt;/li&gt;
&lt;li&gt;Parses the token &lt;code&gt;"25"&lt;/code&gt; into the integer value &lt;code&gt;25&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Returns &lt;code&gt;25&lt;/code&gt; to the program
&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;p&gt;Buffer status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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




&lt;h4&gt;
  
  
  Step 3: Prompt Appears
&lt;/h4&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 plaintext"&gt;&lt;code&gt;Enter an age
25
Enter a name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Misconception:&lt;/strong&gt; 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;&lt;strong&gt;Reality:&lt;/strong&gt; The program pauses only if the buffer is empty.&lt;/p&gt;




&lt;h4&gt;
  
  
  Step 4: &lt;code&gt;nextLine()&lt;/code&gt; Executes
&lt;/h4&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;Checks the buffer&lt;/li&gt;
&lt;li&gt;Immediately detects &lt;code&gt;'\n'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Consumes and discards the line feed&lt;/li&gt;
&lt;li&gt;Returns everything before &lt;code&gt;'\n'&lt;/code&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;h4&gt;
  
  
  Step 5: Final Output
&lt;/h4&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;Output:&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;h2&gt;
  
  
  Solution
&lt;/h2&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.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. The Quick Fix
&lt;/h3&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;h4&gt;
  
  
  Why It Works
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&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;/li&gt;
&lt;li&gt;The additional &lt;code&gt;nextLine()&lt;/code&gt; consumes that &lt;code&gt;'\n'&lt;/code&gt; and removes it.&lt;/li&gt;
&lt;li&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;/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;.&lt;/p&gt;

&lt;p&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;h4&gt;
  
  
  Why It Works
&lt;/h4&gt;

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

&lt;h4&gt;
  
  
  Other Parsing Methods
&lt;/h4&gt;

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




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

&lt;p&gt;Reverse the order of the inputs.&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;h4&gt;
  
  
  Why This Is Wrong
&lt;/h4&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

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

&lt;p&gt;In Java, nothing is broken. The behaviour is defined. The rules are strict. The machine is consistent.&lt;/p&gt;

&lt;p&gt;Use that consistency to your advantage rather than fighting against it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try This Yourself&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;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;Input:&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;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 Aditya Sinha
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pause and think:&lt;/p&gt;

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




&lt;h2&gt;
  
  
  Glossary: Short and Simple
&lt;/h2&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 buffer.&lt;/p&gt;

&lt;p&gt;It serves as a waiting room for data and typically follows the FIFO (First In, First Out) principle.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Source: Keyboard&lt;/li&gt;
&lt;li&gt;Destination: Java program&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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 token is a continuous sequence of non-delimiter characters.&lt;/p&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;5&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;A&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;h&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;i&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&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 json"&gt;&lt;code&gt;&lt;span class="s2"&gt;"25"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;"Ahi"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Parsing
&lt;/h3&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 plaintext"&gt;&lt;code&gt;25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Internally (represented in binary form):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'2'  -&amp;gt; ASCII value 50  
'5'  -&amp;gt; ASCII value 53  
'\n' -&amp;gt; ASCII value 10
&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;
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 &lt;code&gt;nextInt()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"25"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;String&lt;/code&gt; token &lt;code&gt;"25"&lt;/code&gt; is formed.&lt;/p&gt;

&lt;p&gt;The actual parsing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks if &lt;code&gt;"25"&lt;/code&gt; is a valid integer&lt;/li&gt;
&lt;li&gt;Converts it into the numeric value &lt;code&gt;25&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"25"  -&amp;gt; before parsing (String)
25    -&amp;gt; after parsing (int)
&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;Parsing here simply means converting characters into a meaningful data type.&lt;/p&gt;

&lt;p&gt;This is different from compiler parsing, which analyses the grammatical structure of an entire program.&lt;br&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 whitespace character 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;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;' '&lt;/code&gt; (space)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'\t'&lt;/code&gt; (tab)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'\n'&lt;/code&gt; (line feed)
&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;"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;span class="s"&gt;"Age"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\n"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"Ahi"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\t"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"25"&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;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 escape sequence is a special character representation used inside a string. It begins with a backslash (&lt;code&gt;\&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;\n&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;\t&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\"&lt;/code&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;"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;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;ul&gt;
&lt;li&gt;
&lt;code&gt;\"&lt;/code&gt; → Escape sequence, not whitespace&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\n&lt;/code&gt; → Escape sequence and whitespace&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;' '&lt;/code&gt; → Whitespace, not escape sequence&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 delimiter 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 plaintext"&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 plaintext"&gt;&lt;code&gt;[25] [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 java"&gt;&lt;code&gt;&lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;useDelimiter&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;Now the comma becomes the delimiter.&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 csvs"&gt;&lt;code&gt;&lt;span class="mf"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;Ahi&lt;/span&gt;
&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 plaintext"&gt;&lt;code&gt;[25] [Ahi]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Line Terminator
&lt;/h3&gt;

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

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;'\n'&lt;/code&gt; (Line feed)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'\r'&lt;/code&gt; (Carriage return)
&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;



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