CodeGym /Java Blog /Java Classes /Java Scanner Class
Author
Milan Vucic
Programming Tutor at Codementor.io

Java Scanner Class

Published in the Java Classes group
Hi! Our lesson today will be special! In today's lesson, we'll talk about Java Scanner Class. Previously, the process of completing tasks and writing programs was simple: we write some code, run the main() method, the program does what is required, and we're done. But now everything will change! Today we'll learn how to really interact with the program: we'll teach it how to respond to our actions! Before we start analyzing the code, have you ever had to deal with a device like a scanner? Probably. The insides of a scanner are quite complicated, but the basic idea of how it works is very simple: it reads data that the user provides (such as a passport or insurance policy) and stores this information in memory (for example, as an image). Today you're going to create your own scanner! Of course, it won't be able handle paper documents, but text will be no problem for it :) Let's go!

Java's Scanner class

Scanner class - 1First and foremost, we must get acquainted with the java.util.Scanner class. Its functionality is very simple. Like a real scanner, it reads data from a source that you specify. For example, a string, a file, the console. Next, it recognizes the information and processes it appropriately. Here's the simplest example:

public class Main {

   public static void main(String[] args) {

       Scanner scanner = new Scanner("It matters not how strait the gate,\n" +
               "How charged with punishments the scroll,\n" +
               "I am the master of my fate,\n" +
               "I am the captain of my soul");
       String s = scanner.nextLine();
       System.out.println(s);
   }
}
We've created a scanner object, and specified its data source (a string of text). The nextLine() method accesses the data source (our text with a quatrain), finds the next unread line (which is the first line in this case), and returns it. Then we display it on the console: Console output:

It matters not how strait the gate,
We can use the nextLine() method several times and display the entire poem excerpt:

public class Main {

   public static void main(String[] args) {

       Scanner scanner = new Scanner("It matters not how strait the gate,\n" +
               "How charged with punishments the scroll,\n" +
               "I am the master of my fate,\n" +
               "I am the captain of my soul");
       String s = scanner.nextLine();
       System.out.println(s);
       s = scanner.nextLine();
       System.out.println(s);
       s = scanner.nextLine();
       System.out.println(s);
       s = scanner.nextLine();
       System.out.println(s);
   }
}
Each time, our scanner takes one step forward and reads the next line. The program's output is displayed:

It matters not how strait the gate, 
How charged with punishments the scroll, 
I am the master of my fate, 
I am the captain of my soul
As we've already said, the scanner's data source doesn't have to be a string. For example, it could be the console. Some exciting news for you: previously, we just displayed data there, but now we'll read data from the keyboard! Let's see what else the Scanner class does:

public class Main {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a number:");

       int number = sc.nextInt();

       System.out.println("Thanks! You entered the number " + number);

   }
}
The nextInt() method reads and returns the entered number. In our program, we use it to assign a value to the variable number. It's already more like a real scanner! The program asks the user to enter any number. After the user has done this, the program thanks the user, displays the result, and finishes. But we still have a serious problem. The user might make a mistake and enter something wrong. Here is an example where our current program stops working:

public class Main {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a number:");

       int number = sc.nextInt();

       System.out.println("Thanks! You entered the number " + number);

   }
}
Let's try entering the string "CodeGym" instead a number: Console output:

Enter a number: 
CodeGym 
Exception in thread "main" java.util.InputMismatchException 
at java.util.Scanner.throwFor(Scanner.java:864) 
at java.util.Scanner.next(Scanner.java:1485) 
at java.util.Scanner.nextInt(Scanner.java:2117) 
at java.util.Scanner.nextInt(Scanner.java:2076) 
at Main.main(Main.java:10) Process finished with exit code 1
Uh, oh. We're in big trouble -_- To avoid such situations, we need to come up with a way to verify the data entered by the user. For example, if the user enters anything other than a number, it would be good to display a warning that the entered information is not a number. And if the information is okay, then we could confirmation. But this would require us to "look into the future" to see what's coming in our stream. Can Scanner do this? And how! And it has a slew of methods for doing this: hasNextInt() — This method checks whether the next chunk of input data is a number (returns true or false, as appropriate). hasNextLine() — This method checks whether the next chunk of input is a string. hasNextByte(), hasNextShort(), hasNextLong(), hasNextFloat(), hasNextDouble() — All these methods perform similar checks for the remaining data types. Let's try changing our number-reading program:

public class Main {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a number:");

       if (sc.hasNextInt()) {
           int number = sc.nextInt();
           System.out.println("Thanks! You entered the number " + number);
       } else {
           System.out.println("Sorry, but this is clearly not a number. Restart the program and try again!");
       }

   }
}
Now our program checks whether the next character entered is a number. And it displays confirmation only if it is. If the input doesn't pass the check, the program takes note and asks the user to try again. Basically, you can communicate with the Scanner object and find out in advance what data type awaits you. A number, string, or something else? A number? And what kind? An int, short, long?" This flexibility gives you the opportunity to build program logic that depends on the user's behavior. We should make note of another important method: useDelimiter(). You pass a string to this method. The string contains the characters you want to use as separators or delimiters. For example, suppose we suddenly became interested in Japanese poetry, and decided to use our scanner to read some haiku written by the great poet Matsuo Bashō. Even if three different verses are passed to us as one awkward string, we can easily split them and render them beautifully:

public class Main {
   public static void main(String[] args) {
       Scanner scan = new Scanner("On a withered branch'" +
               "A crow has alighted.'" +
               "Nightfall in autumn." +
               "''***''" +
               "Such a moon above,'" +
               "Like a tree cut at the root:'" +
               "he fresh cut is white." +
               "''***''" +
               "How the river floods!'" +
               "A heron wanders on short legs,'" +
               "Knee-deep in the water.");

       scan.useDelimiter("'");

       while (scan.hasNext()) {
           System.out.println(scan.next());
       }

       scan.close();
   }
}
We use "\n/*/*/*" (new line character and three asterisks) as our delimiter. As a result, we have beautiful console output, just like in books:
On a withered branch 
A crow has alighted. 
Nightfall in autumn. 

*** 

Such a moon above, 
Like a tree cut at the root: 
The fresh cut is white. 

*** 

How the river floods! 
A heron wanders on short legs, 
Knee-deep in the water.
This example has one more method that we must absolutely point out: close(). Like any object working with I/O streams, the scanner must be closed when it is done so it stops consuming the computer's resources. Never forget the close() method!

public class Main {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a number:");

       int number = sc.nextInt();

       System.out.println("Thanks! You entered the number " + number);
      
       sc.close(); // Now we've done everything right!

   }
}
That's it! As you can see, for how helpful it is, the Scanner class is quite easy to use! :) To reinforce what you learned, we suggest you watch a video lesson from our Java Course
Comments (70)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jesú Level 14, Madrid, Spain
13 October 2023
Great lesson
Stroidzz Level 7, Discovery, South Africa
16 April 2023
Either you are not familiar with a scanner at all, or both you and the scanner are familiar with each other, you are well acquainted AND you know each other's secrets.
Lunita Level 4, Dominican Republic
24 January 2023
Java Scanner Class — bookmarked! Notes to self: Declaring Scanner object

Scanner sc = new Scanner("I am the master of my fate,\n" +
               "I am the captain of my soul"); 
String s = scanner.nextLine();
System.out.println(s);
//solo imprime la primera linea, para las demas poner:
String s = scanner.nextLine();
System.out.println(s);
s = scanner.nextLine();
System.out.println(s); // etc etc

Para enteros Int

Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int number = sc.nextInt(); // hay mas metodos para cada tipo
System.out.println("Thanks! You entered the number " + number);

Scanner Methods:

nextInt()
nextLine()
hasNextByte(), hasNextShort(), hasNextLong(), hasNextFloat(), hasNextDouble()

ej:

Scanner sc = new Scanner(System.in);
       System.out.println("Enter a number:");

       if (sc.hasNextInt()) {
           int number = sc.nextInt();
           System.out.println("Thanks! You entered the number " + number);
       } else {
           System.out.println("Sorry, this is not a number. Try again!");
       }

Método useDelimiter() A este método se le pasa una cadena. La cadena contiene los caracteres que desea utilizar como separadores o delimitadores.

Ej. sc.useDelimiter("'");

Al final del programa se cierra el Scanner. Nunca olvidar este paso.

sc.close();
Michał Level 7, Przeworsk, Poland
17 December 2022
once Scanner is class, once is object, I am confused
Brandon Jaeger Level 4, United States
21 September 2022
Why am I getting "cannot declare variable here and "if" cannot exist without "else" errors in this here: System.out.println("enter number") ; Scanner scan= new Scanner(System.in) ; if (scan.hasNextInt()) int ggg = nextInt(scan) ; System.out.println("Awesome") ; else System.output.println("Nope"); scan.close();
Molo Level 41, Lublin, Poland
16 August 2022
Note the case-sensitive nature of the function that checks vs. writes to the variable if (sc.hasNextInt()) int number = sc.nextInt();
Anonymous #10865265 Level 9, Zhengzhou, China
9 November 2021
I'm learned a lot.
Jaime Padilla Level 32, Chandler, United States Expert
9 April 2021
For those asking about the Delimiter, the article seems to reference the /n command and three asterisks but your delimiter is actually a single String quote '. Anytime the program seems a single quote it will split/space the group of words. Look at the single quote behind each sentence. When you get to the three asterisks you can see that there are four single quotes used to give greater separation between the poems. Maybe someone else can clarify the /n and three asterisks the article talks about, I'm still new.
Piotr Level 17
15 December 2020
there isn't anywhere explained why do I ever need to use scanners instead of just declaring String but hope I will find out at the right time
Chris J. Level 19, Arlington, United States Expert
12 December 2020
scan.useDelimiter("\n/*/*/*") why are there forward slashes before the asterisks?