There
are different ways to read the Integer values from the files
Intergr.parsent():
The
common way used by all are the reading character by character from the files
and convert that value to integer and catching the NumberFormat exception for non-integer values.
import
java.io.FileInputStream;
import java.io.IOException;
public class convertInt {
public static void main(String[] args) throws IOException {
FileInputStream
file = null;
char cBuf;
file
= new FileInputStream("Test.txt");
while(file.available()
> 0) {
cBuf
= (char) file.read();
try {
Integer.parseInt(""+cBuf);
System.out.print(cBuf);
}
catch
(NumberFormatException e) {
}
}
}
}
Text.txt:
Employee
Number: 786798
O/p:
786798
Disadvantage with above approach:
- It is not a good practice to catch runtime exception. (NumberFormatException is a runtime exception)
- If we are catching one exception need to handle it correctly. Empty catch blocks are not good approach.
- If we used the above approach in applications and projects, later in some time if code clean or if they introduce the code check list to clean up the code, most of the check list contain enter log for catch block and during mass change in each code they will right error log for this above NumberFormatException.And later in log file it will create confusion for all. And also it will increase log size that will decrease performance.
So
don’t use this method in large applications.
Matching
String:
Using
pattern matching we can make Array of {‘0’,’1’,’2’ .. ‘9’} and check each file
characters with this Array.
import java.util.ArrayList;
import java.util.Arrays;
public class convertInt {
public static void main(String[] args) throws IOException {
FileInputStream
file = null;
char cBuf;
String[]
arrayNum = {"0","1","2","3","4","5","6","7","8","9"};
file
= new FileInputStream("test.txt");
ArrayList
aList = new ArrayList<String>(Arrays.asList(arrayNum));
while(file.available()
> 0) {
cBuf
= (char) file.read();
if(aList.contains(""+cBuf))
{
System.out.print(cBuf);
}
}
}
}
Creating a String array (arrayNum) and converting that to list using: Arrays.asList(arrayNum) method
Since List has contains method, we can easily check.
Disadvantage:
We are creating a separate array and making a list, so unnecessarily using memory.
Convert
to TOASCII and check:
This
is a good approach compare to above to approach
public class AsciiNumCheck {
public static void main(String[] args) throws IOException {
FileInputStream
file = null;
char cBuf;
int i ;
file
= new FileInputStream("Test.txt");
while(file.available()
> 0) {
cBuf
= (char) file.read();
i = (int) cBuf;
if( i >= 48
&& i <= 57) {
System.out.print(cBuf);
}
}
}
}
The i = (int) cBuf; will convert the character to ASCII value.
The Ascii value for 0 is = 48 and for 9 is = 57
{0,1,2 … 9 } for ASCII are continuous values from {48,49,50 … 57}
So integer will come between these values.
import
java.util.InputMismatchException;
import java.util.Scanner;
public class ScannerIntCheck {
public static void main(String[] args) throws
FileNotFoundException {
Scanner
scanner = new Scanner(new File("Test.txt"));
scanner.useDelimiter
(System.getProperty("line.separator"));
String
line = "";
while(scanner.hasNext())
{
line
= scanner.next();
Scanner
lineScanner = new Scanner(line);
while(lineScanner.hasNext())
{
try {
System.out.println(lineScanner.nextInt()+" ");
}
catch
(InputMismatchException exce) {
lineScanner.next();
}
}
}
}
}
Using scanners we
will be able to read values from files. But only issue with Scanner is that it need delimiter to
read the values.
Here first we read
each line using line.separator as delimiter. After
that we separate each values in line using ‘space’, that is default separator,
no need to specify delimiter as space.
Here if the value is
not number nextInt() will throw
InputMismatchException as runtime exception.
This is good in with file
contain delimiter separated values.
I/P:
443 45wer
343 5
455 8
Ouput:
443 343 5 455 8