How to Read Multiple Numbers in Java

How to Read Multiple Numbers in Java thumbnail
Java programs can read numbers, text and other data from files.

Java provides a range of classes you can use within your programs when you need to read external data. The Scanner class allows you to read input from files, including number values. Using this class in conjunction with loops, your Java programs can read multiple items of data. The Scanner class can read individual numbers including double precision, floating point, short and integer types. You can therefore choose a method to suit your data, placing your code within a loop so that it can read multiple numbers when the program executes.

Instructions

    • 1

      Import the Java input and output resources you need to read your external data. Add the following import statements at the top of your class declaration:

      //input output library
      import java.io.*;
      //scanner class
      import java.util.Scanner;

      These resources give your program everything it needs to process and handle input from external sources such as text files.

    • 2

      Create "try" and "catch" blocks for your number reading operation. When you use input and output resources in Java, you need to prepare for any errors that may occur if your external data cannot be read. Add the following code to your program:

      try {
      //number reading here
      }
      catch(catch(IOException numReadExc) {
      System.out.println("Exception: " + numReadExc.getMessage());
      }

      When your code runs, execution will immediately move to the catch block if something goes wrong with the input operation. You can place your file reading code inside the try block.

    • 3

      Instantiate the input objects necessary for your number reading process. Add the following code inside your try block, declaring and instantiating an object of FileReader type:

      FileReader numFileReader = new FileReader("datafile.txt");

      Alter the parameter to the constructor method to match the name and location of your data file relative to your program. Add the following code, creating an object of the BufferedReader class:

      BufferedReader numBuffer = new BufferedReader(numFileReader);

      This class takes an instance of the FileReader class as a parameter. Add the following code, declaring and instantiating a Scanner object:

      Scanner numScan = new Scanner(numBuffer);

      The Scanner constructor takes a BufferedReader as a parameter.

    • 4

      Create a loop for your number reading procedure. Inside your try block, after your object instantiation code, add the following loop outline:

      while(numScan.hasNext()) {
      //read numbers here
      }

      This loop will only continue to execute while your file still has data to read. Inside the loop, you can add the code instructing the Scanner to read numbers from the external source. When your code finishes reading everything in the file, the loop will stop executing.

    • 5

      Read numbers from your file. You need to choose the right Scanner method for your number types. The following sample code will read integers inside the loop:

      int currInt = numScan.nextInt();

      You can choose from methods to read double, float, long, short, BigInteger and BigDecimal numbers. You can carry out any processing needed on your numbers inside the loop. After the loop, close your Scanner as follows:

      numScan.close();

      Save the file and run your program to test it.

Tips & Warnings

  • The Scanner class can also read lines of text and boolean values.

  • Your code may encounter errors if you attempt to read the wrong type of numbers from a file.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured