Extracting Integers From Text file

Extracting Integers From Text file

Question:
How do I read a series of integers from an ASCII text file? The file contains a sequence of numbers like

110 120 130 140 ... 

I tried to use readInt() andDataInputStream, but all it returned were some random values.

Answer:
DataInputStream is designed to read the binary representation of intrinsic Java types from an input stream. The readInt method reads 4 bytes and interprets them as a big endian two’s complement integer. You wound upwith apparently random values because the integers you were trying to read were stored as ASCII text.

The only way to read numbers stored in an ASCII text file is to read them as strings and then convert them to Java integers. One approach is toread the input line by line, tokenize the input yourself, and use Integer.parseInt to convert the strings to Java integers. This requires that you write quite a bit of extra code.

A second approach is to use the StreamTokenizer class, which already does most of the work for you. A StreamTokenizer will read whitespace separated tokens from an InputStream or reader and optionally parse numerical input for you. The class is not elegantly designed, but gets the job done for this common case. Eachtoken is read sequentially from the stream by calling nextToken(), which returns the type code of the token. A type code of StreamTokenizer.TT_EOF indicates that the end of the input has been reached. Youcan tell the class to parse numbers for you by calling parseNumbers(). Number tokens are identified by the TT_NUMBER constant and are retrieved by reading the public variable nval.Number parsing is generalized to include both integers and floating point, so the nval variable is of typedouble. String tokens are stored in sval. This ad hoc method of accessing token values is why I called the classinelegant, but it works.

The following code listing demonstrates how to use the StreamTokenizer class. It reads a series of numbers from a file, adds them up, and prints out the total. I also provide sample input for you to test the program.

import java.io.*;/*** * This program reads a list of whitespace separated numbers from * a text file, adds them up, and prints the total. It demonstrates * how to use the StreamTokenizer class. You can use the sample * ReadNumbers.txt file as input. ***/public class ReadNumbers {  public static void main(String[] args) {    int tokenType;    FileReader input;    StreamTokenizer tokenizer;    double total;    if(args.length != 1) {      System.err.println("Usage: ReadNumbers ");      return;    }    try {      input = new FileReader(args[0]);    } catch(FileNotFoundException e) {      System.err.println("File not found.");      return;    }    tokenizer = new StreamTokenizer(input);    // Tell the tokenizer to parse numbers for us    tokenizer.parseNumbers();    total = 0.0;    try {      while((tokenType = tokenizer.nextToken()) !=	    StreamTokenizer.TT_EOF) {	if(tokenType != StreamTokenizer.TT_NUMBER) {	  System.err.println("Invalid input file!  Only expecting numbers.");	  return;	}	total+=tokenizer.nval;      }    } catch(IOException e) {      System.err.println("Error reading next token.");      return;    }    System.out.println("Total: " + total);  }                   }

10.12     20.0   30 40  91   22.5213    56 78.42    92.88  17.001
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as