Java

Java Scanner Class with Examples using NetBeans

Java Scanner Class

Java Scanner Class with Programming Examples using NetBeans- In Java programming, there are several classes that we can use to take input from the user, namely Scanner, BufferedReader, and JOptionPane, these three classes have the same function, but in JOptionPane, this class has a visual display or GUI. An application project certainly requires input data from the user to be processed into output, in Java we can create a program that allows the user to input data in a variable and print the results on the monitor screen.


In this tutorial, we will learn Java Scanner and its methods with the help of examples using netbeans.

The PrintWriter class of the java.io package can be used to write output data in a normally readable form (text).

The Scanner class of the java.util package is used to read input data from different sources such as input streams, users, files, etc. Let’s take an example.

To use a java Scanner class, we need to create an object from the java Scanner class, the class is in the package java.util.*, so we need to import the package first, after the package and object have been created, then we need a variable to hold the value input from users. We will make a simple program using a scanner, in the program the user is asked to input something, and the results will be printed using System.out.println() , try to look at the following example program.



Example 1: Using a java scanner class to read a line of text

output:

Java Scanner Class

In the above example, notice the following line

Here, we create a Scanner object called input.

The System.in parameter is used to get input from standard input. Like getting input from the keyboard.

To store String data, we can use the next() or  nextLine() method of the java Scanner class, in addition to the String data type, there are several other methods that you can use of the java Scanner class, including:

  • nextInt – Accepts integer/Integer input
  • nextBoolean – Accepts Boolean input (true/false)
  • nextFloat – Accepts fractional input/Float
  • nextDouble – Accepts fractional/Double input
  • nextByte – Receives an integer/Byte
  • nextLong – Receives an integer/Long
  • nextShort – Accepts an integer/Short


Example 2: how to get number data type Integer using java scanner class

output:

Java Scanner Class

Now that you know something about java Scanner class, let’s explore it a little more.

Import the java scanner class

As you can see from the above examples, we need to import the java.util.Scanner package before we can use the Scanner class.


Creating Scanner Objects in Java

As mentioned above, once the package is imported, the Scanner object can be created.

Here we have created objects of the Scanner class that will read input from InputStream, File, and String respectively.

Java scanner class input methods

The java Scanner class provides various methods that allow us to read out different types of input.

Method Description
nextInt() read int value from the user
nextFloat() read float value from the user
nextBoolean() read the boolean value from the user
nextLine() Read a line of text from the user
next() Read a word from the user
nextByte() Read byte value from the user
nextDouble() read the double value from the user
nextShort() read the short value from the user
nextLong() read the long value from the user


Example 3: nextInt() Method of the Java Scanner Class

The nextInt() method is used to get the integer values from the user. So in the below example,  I am using the nextInt() method in programming.

output:

Java Scanner Class



Example 4: How to use nextDouble() method of the Java Scanner Class

The nextDouble() method is used the get the floating-point values from the user. So in the below example,  I am using the nextDouble() method in programming.

output:

Java Scanner Class

Example 5: next() method of the Java Scanner Class

The next() method is used to get a String from the user. in the below example,  I am using the next() method in programming.

output:

Java Scanner Class

Here, we provide the full string. However, the next() method only reads the string.

This is because the next() method reads the input up to a space character. As soon as a space is encountered, it returns the string (excluding spaces). As you can see in the below image only Programming is displayed.

Java Scanner Class


Example 6: nextLine() method of the Java Scanner Class

output:

Java Scanner Class

In the first example, we used the nextLine() method to read the string from the user.

Unlike next() method, the nextLine() method reads the entire input line including spaces. Terminates when the method encounters the next line character \n.

Java Scanners BigInteger and BigDecimal

The Java scanner can also be used to read big integers and big decimal numbers.

  • nextBigInteger() – reads a big integer value from the user
  • nextBigDecimal() – reads the big decimal value from the user


Example 7: Reading BigInteger and BigDecimal numbers using Java Scanner

In the above example, we use the java.math.BigInteger and java.math.BigDecimal packages to read BigInteger and BigDecimal respectively.

output:

Java Scanner Class

In the above example, we use the java.math.BigInteger and java.math.BigDecimal packages to read BigInteger and BigDecimal respectively.


Example 8: how to create Static Login program using Java Scanner Class:

Output:

When you enter the wrong username or password it will show the following output

Java Scanner Class

And when you enter the correct username and password, then it will show the following output

Java Scanner Class

Example 9: How to create an array in java by taking input from the console:

Output:

Java Scanner Class



Example 10: How to use java scanner Class in switch case statement:

Output:

Java Scanner Class

Java Scanner class Vs java BufferedReader class

In this section, we will use the BufferedReader class, this class is in the java.io package , this class is used to get input from the user, almost the same as Scanner, the only difference is the way of writing the syntax.

To use this class, we must first import the following packages:

To make it easier, you can write it like this:

In the following example, there we create an object of the BufferedReader class, to get input from the user,  and then I used the readLine() method to read the data, and don’t forget to add a try-catch just in case something goes wrong during the input process.


Example 11: how to use BufferedReader class in java to get input from the user:

output:

Java Scanner Class

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button