<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>User input/output in java | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/user-input-output-in-java/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Thu, 16 Feb 2023 07:42:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>User Input/Output</title>
		<link>https://studentprojects.in/software-development/java/user-input-output-2/</link>
					<comments>https://studentprojects.in/software-development/java/user-input-output-2/#respond</comments>
		
		<dc:creator><![CDATA[Shubhajna Rai]]></dc:creator>
		<pubDate>Mon, 27 Feb 2023 07:38:53 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[User input/output in java]]></category>
		<guid isPermaLink="false">https://studentprojects.in/?p=10208</guid>

					<description><![CDATA[<p>Taking input:  Getting Started: We must import the java.util.Scanner package in order to use the Scanner class. import java.util.Scanner; Recently, the Scanner class was imported. To use the Scanner class&#8217;s methods, we construct an object of that class. Scanner sc = new Scanner(System.in) The System.in is passed as a parameter and is used to take</p>
<p>The post <a href="https://studentprojects.in/software-development/java/user-input-output-2/">User Input/Output</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p><strong>Taking input: </strong></p>



<p>Getting Started: We must import the java.util.Scanner package in order to use the Scanner class.</p>



<p>import java.util.Scanner;</p>



<p>Recently, the Scanner class was imported. To use the Scanner class&#8217;s methods, we construct an object of that class.</p>



<p>Scanner sc = new Scanner(System.in)</p>



<p>The System.in is passed as a parameter and is used to take input.</p>



<p>Note: Your object can be named anything, there is no specific convention to follow. But we normally name our object sc for easy use and implementation.</p>



<p><strong>An object of the scanner class can be created in four different ways. Let&#8217;s look at how to generate scanner objects.</strong></p>



<ol type="A">
<li>Reading keyboard input:</li>
</ol>



<p>Scanner sc = new Scanner(System.in)</p>



<ul>
<li>Reading String input:</li>
</ul>



<p>Scanner sc = new Scanner(String str)</p>



<ul>
<li>Reading input stream:</li>
</ul>



<p>import java.util.Scanner;</p>



<ul>
<li>Reading File input:</li>
</ul>



<p>Scanner sc = new Scanner(File file)</p>



<p><strong>How does a Scanner class operate then?</strong></p>



<p>The inputs are read by the scanner object, which then tokenizes the string. Whitespaces and newlines are the usual criteria used to split the tokens.</p>



<p>Example:</p>



<div class="is-layout-constrained wp-block-group"><div class="wp-block-group__inner-container">
<p>Susan</p>



<p>19</p>



<p>78.95</p>



<p>O</p>
</div></div>



<p>The input above will be separated into the letters &#8220;Susan,&#8221; &#8220;19,&#8221; &#8220;78.95,&#8221; and &#8220;O.&#8221; Depending on the type of Scanner object produced, the Scanner object will then iterate over each token and read it.</p>



<p>Now that we&#8217;ve seen how the Scanner object is made, we can see the various ways it may be made, how it reads input, and how it functions. Let&#8217;s examine various approaches to gathering input.</p>



<p><strong>The scanner class&#8217;s methods are listed below:</strong></p>



<figure class="wp-block-table"><table><tbody><tr><td><strong>Method</strong></td><td><strong>Description</strong></td></tr><tr><td>nextLine()</td><td>Accepts string value</td></tr><tr><td>next()</td><td>Accept string till whitespace</td></tr><tr><td>nextInt()</td><td>Accepts int value</td></tr><tr><td>nextFloat()</td><td>Accepts float value</td></tr><tr><td>nextDouble()</td><td>Accepts double value</td></tr><tr><td>nextLong()</td><td>Accepts long value</td></tr><tr><td>nextShort()</td><td>Accepts short value</td></tr><tr><td>nextBoolean()</td><td>Accepts Boolean value</td></tr><tr><td>nextByte()</td><td>Accepts Byte value</td></tr></tbody></table></figure>



<p>Example:</p>



<pre class="wp-block-code"><code lang="java" class="language-java">import java.util.Scanner;

public class ScannerExample {
	public static void main(String[] args) {
		
        Scanner sc = new Scanner(System.in);
 
        System.out.println("Enter Name, RollNo, Marks, Grade");
        
        String name = sc.nextLine();		//used to read line
        int RollNo = sc.nextInt();			//used to read int
        double Marks = sc.nextDouble();		//used to read double
        char Grade = sc.next().charAt(0);	//used to read till space
 
        System.out.println("Name: "+name);
        System.out.println("Gender: "+RollNo);
        System.out.println("Marks: "+Marks);
        System.out.println("Grade: "+Grade);
        
        sc.close();
	}
}</code></pre>



<p>Output:</p>



<pre class="wp-block-code"><code lang="java" class="language-java">Enter Name, RollNo, Marks, Grade
Mohit
19
87.25
A
Name: Mohit
Gender: 19
Marks: 87.25
Grade: A</code></pre><p>The post <a href="https://studentprojects.in/software-development/java/user-input-output-2/">User Input/Output</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/java/user-input-output-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
