Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Tokenization with Scanner in Java

See in this article, why the importance of tokens in the search and "breaks" in large text within text files or any other type of storage of Strings with the help of the Scanner class.

There are situations where we, programmers, received the "simple" task of opening a file, reading a text with 50 thousand words, break the text into smaller pieces and use these parts to configure Beans and stuffs like that. In cases like this one should not reinvent the wheel and try to create something totally revolutionary that will change the way all programmers perform this task. Actually, yes, you can try to do this. But know that it will be "wasting time", since a solution for this already exists and is very simple to use. Java offers its programmers tokens that basically perform all the tasks listed above.

Using the previous scenario, suppose we need to open a file called test.txt, do a read in your text, break it into parts and assign them to certain objects to be used later in other parts of our system. Sounds complicated, right?! But it is not.

We will discuss one of the classes that offer this type of service: Scanner.

Scanner

Before we attempt to solve the above problem, we'll take a look at the example in the book that there Scanner certification in order to exemplify well and absorb the concept of class.

Listing 1: Sample Book Scanner

import java.util.Scanner;
 
 
public class Testando {
 
    public static void main(String[] args)
    {
        boolean b2, b;
        int i;
        String s, hits = "";
         
        // Creating Scanners.
        Scanner s1 = new Scanner(args[0]);
        Scanner s2 = new Scanner(args[0]);
         
        // Verifying the first scanner
        while (b = s1.hasNext())
        {
            s = s1.next();
            hits += "s";
        }
         
        // Verifying the second scanner
        while (b = s2.hasNext())
        {
            if (s2.hasNextInt())   // The next is a integer?
            {
                i = s2.nextInt();
                hits += "i";
            }else if (s2.hasNextBoolean()) // The next is a Boolean?
            {
                b2 = s2.nextBoolean();
                hits += "b";
            }else
            {
                s2.next();              // Or none of the above?
                hits += "s2";
            }
        }
         
        System.out.println("hits " + hits);
    }
}

Bearing in mind that the arguments passed to the class earlier were "34 1 true hi", the console will reset the "hits ssssibis2."

Understanding the flow of the sample book

  • By default, the default delimiter is space Scanner. Thus, every space found in the String, a new value will be separated by Scanner. This delimiter can be changed by other types, say, more specific.
  • Two scanners were created, both receiving the value of the first slot of the vector of the arguments that came.
  • From the first scanner was checked if there was any value within the string. That is, if the Scanner found any any value using the space delimiter, enter the while until the latter was found.
  • Using the second scanner, a check was made more specific in String. When the next value to be used was a Integer, a letter "i" was to be added to variable hints; When I was a boolean, a letter "b" When was neither Integer and Boolean or an "s2".
  • With that, we got the result "ssssibis2 hits."

The example served to show a basic use and other more specific class Scanner.

Solving the problem proposed in the article:

To solve the problem posed at the beginning of the article, we could use String.split (). But it is more practical and efficient scanner that we use due to its diversity of features that assist the programmer at the time of the operation. So hands dirty!

We will have a file called test.txt that has as content, text, "Tiago 23 Argolo".

Listing 2: Creating the test.txt file and setting its content

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
 
public class Tester {
     
    public static void main(String[] args) {
 
        File f = new File("Teste.txt");    
         
        try
        {
            FileWriter fw = new FileWriter(f);   // Create our file
            fw.append("Tiago 23 Argolo");        // Set the content.
            fw.flush();
            fw.close();
        }catch(IOException e){}
             
         
    }
 
}

Before you utilize the Scanner in this class, is a summary of the book's Java certification explaining its features:

  • Scanners can be built using files, strings or streams as a source.
  • The tokenization is performed inside a loop, so you can terminate the process at any time.
  • Tokens can be converted automatically to their appropriate primitive types.

Scanner in practice!

We begin with using the Scanner class, then.

Listing 3: Using the Scanner class Teste.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
 
 
public class Tester {
     
    public static void main(String[] args) {
 
        File f = new File("Teste.txt");
        String linha = ""; String inteiro = ""; String texto = "";
        Scanner s;
         
        try
        {
            FileWriter fw = new FileWriter(f);   // Create our file
            fw.append("Tiago 23 Argolo");        // Set the content.
            fw.flush();
            fw.close();
        }catch(IOException e){}
             
        // Getting your information and file
         
        try
        {
            FileReader fr = new FileReader(f);         
            BufferedReader br = new BufferedReader(fr);
            linha = br.readLine();
             
            s = new Scanner(linha);
            boolean b;
             
            while (b = s.hasNext()) // checks if it has another value.
            {
                if (s.hasNextInt())   // checks if the next value is a integer 
                {
                    inteiro += " " + s.next();
                }else               //   if not Integer
                {
                    texto += " " + s.next();
                }
            }
              
        }catch(IOException e){}
 
        //From here, I can use both variables in other objects
      //  that will be used by my application.
         
        System.out.println("Textos" + texto +"\nInteiros: " + inteiro);
    }
 
}

In this code were used the most practical ways so that the explanation can also be simpler. So do not be surprised if you find something valid, but not recommended in class Teste.java

Explaining the class code Teste.java

  • Scanner was created with a String object that contains the contents of the file test.txt
  • With this scanner, looked inside the String object if there is a next value.
  • If positive, check if the value is integer or not. If it is Integer, write the value in the integer variable. If anything that isn't Integer, write the value in the variable text.
  • Display both variables at the end.

Here is the result grade Teste.java:

Implementation of class Teste.java

Figure 1: Implementation of class Teste.java

This was an exemplification simple but practical. Imagine using that power in pursuit of something more sophisticated, as an array containing thousands and thousands of lines. It is necessary that this sample is transferred to something more complex. Something that maybe you use in your work and now know how to improve the procedure, making it faster and more efficient.

With that, I conclude my article on using tokenization Scanner, a very important tool in the Java language that can be applied in several distinct situations. I hope I helped in understanding the subject and encouraged their use in day-to-day in your workplace.

Best regards!

References:

Bert Bates, Kathy Sierra. Sun Certified Java Programmer 5 - SCJP (Study Guide)

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html


Oracle Certified Professional, Java SE 6 Programmer. Bacharel em Sistemas de Informação desde 2010. Especialista em Engenharia de Software com foco em Segurança da Informação pelo C.E.S.A.R e cursando o MBA em Gestão Empresarial ...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login