{"id":20441,"date":"2022-09-16T15:19:27","date_gmt":"2022-09-16T09:49:27","guid":{"rendered":"https:\/\/java2blog.com\/?p=20441"},"modified":"2022-09-16T15:19:28","modified_gmt":"2022-09-16T09:49:28","slug":"take-integer-input-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/take-integer-input-java\/","title":{"rendered":"How to Take Integer Input in Java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Variables\">Variables<\/a><\/li><li><a href=\"#What_is_Integer\">What is Integer?<\/a><\/li><li><a href=\"#How_to_take_integer_input\">How to take integer input?<\/a><ul><li><a href=\"#Java_Scanner_class\">Java Scanner class<\/a><\/li><li><a href=\"#Java_Program_to_to_take_Integer_input_in_Java\">Java Program to to take Integer input in Java<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<p>This article discusses the methods to take the integer input in Java.<\/p>\n<h2><span id=\"Variables\">Variables<\/span><\/h2>\n<p>In programs, data values are reserved in memory locations that can be identified by names (identifiers). Such named memory location which temporarily reserves data that can be changed while the program is running is called a <a href=\"https:\/\/java2blog.com\/variables-in-java\/\" title=\"variable\">variable<\/a>. <\/p>\n<p>Let&#8217;s see how variables are declared using Data types, Identifiers \\&amp; Values.<\/p>\n<div class=\"content-box-green\">\ndatatype identifier = value\n<\/div>\n<p>In detail, let&#8217;s look at different data types in the diagram below.<\/p>\n<p>Data types are mainly of two kinds: <\/p>\n<ol>\n<li>Primitive Data Types <\/li>\n<li>Non-Primitive Data Types.<\/li>\n<\/ol>\n<p>In this article, we will see what is integer and how to take integer input in java.<\/p>\n<h2><span id=\"What_is_Integer\">What is Integer?<\/span><\/h2>\n<ul>\n<li>An integer is a primitive data type.<\/li>\n<li><code>int<\/code> keyword is used to define integer data type variable.<\/li>\n<li>Integer data types have the size of 4 bytes.<\/li>\n<li>The range of integer data type is <code>-2^31<\/code> to <code>2^31 \u2013 1<\/code>.<\/li>\n<\/ul>\n<p>For example:<\/p>\n<pre code=\"java\" title=\"Integer syntax\">\nint a = 32;\n<\/pre>\n<h2><span id=\"How_to_take_integer_input\">How to take integer input?<\/span><\/h2>\n<h3><span id=\"Java_Scanner_class\">Java Scanner class<\/span><\/h3>\n<p>We can <a href=\"https:\/\/java2blog.com\/take-input-user-java\/\" title=\"take input as an integer from a user\">take input as an integer from a user<\/a>, with the help of the Scanner class. In <code>java.util<\/code> package Scanner class is present. It can also be used to take input as an integer, short, byte, double, float, string, etc. <\/p>\n<p>By creating an object of the Scanner class we can use any method of the Scanner class.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre code=\"java\" title=\"Scanner syntax\">\nScanner sc = new Scanner(System.in);\n<\/pre>\n<p>The above line of code creates a constructor of the Scanner class. That constructor has an argument as <code>System.in<\/code>. That means it will read from the standard input stream of the program.<code> java.util<\/code> package needs to import while using Scanner class in a program.  <\/p>\n<p>Java Scanner class provides the different methods to read different primitives types. For example: <code>int<\/code>is read using <code>nextInt()<\/code>, <code>float<\/code>is read using <code>nextFloat()<\/code>, <code>double<\/code> is read using <code>nextDouble()<\/code>, and <code>String<\/code> is read using <code>nextLine()<\/code> etc.<\/p>\n<h3><span id=\"Java_Program_to_to_take_Integer_input_in_Java\">Java Program to to take Integer input in Java<\/span><\/h3>\n<p>We need to import <code>java.util.Scanner<\/code> class to use Scanner. To read integer input from the user first need to create an object of Scanner class by passing <code>System.in<\/code>. <\/p>\n<p>Then with the help of <code>nextInt()<\/code> method of the Scanner class, we can store the value in a <code>num<\/code> integer variable. It&#8217;s best practice to close the Scanner object with the help of the <code>close()<\/code> method. Finally, we are printing the value of <code>num<\/code>.<\/p>\n<pre code=\"java\" title=\"Taking Integer input using Scanner\">\nclass Demo\n{\n    public static void main(String args[])\n    {\n        Scanner scan = new Scanner(System.in);\n        System.out.print(\"Enter integer number: \");\n        int num = scan.nextInt();\n        scan.close();\n        System.out.println(\"The number entered by user is: \");\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>While executing the program, it will ask to enter a number in the console. Enter the input in the console. Then it will print the value of the entered number.<\/p>\n<div class=\"content-box-green\">\nEnter integer number: 32<br \/>\nThe number entered by user is: 32\n<\/div>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/global-variables-java\/\" target=\"_blank\" rel=\"noopener\">Global variables in java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/global-variables-java\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/reference-variable-java\/\" target=\"_blank\" rel=\"noopener\">Reference variable in java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/reference-variable-java\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>In this article, we learned how to declare, initialize and take integers from users with the help of the Scanner class. Using the <code>nextInt()<\/code> method of the Scanner class we can take input from the console and store the value in the Integer variable. <\/p>\n<p>This is all about taking an integer input in Java. <\/p>\n<p>Hope you enjoyed reading the article. Stay tuned for more articles. Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsVariablesWhat is Integer?How to take integer input?Java Scanner classJava Program to to take Integer input in JavaConclusion This article discusses the methods to take the integer input in Java. Variables In programs, data values are reserved in memory locations that can be identified by names (identifiers). Such named memory location which temporarily reserves [&hellip;]<\/p>\n","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[68,5],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20441"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=20441"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20441\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=20441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=20441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=20441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}