Although, I am in enginnering line, I enjoy programming a lot (EE has got a connection with programming). In my spare time, I do fun programming. Below is Java code of an FSM (Finite State Machine) for identifying and reading numbers from a text file. It strips every thing off (e.g. white space, line feeds, carriage returns, text, or any character) and just reads numbers (integer and real).
Although not very neatly designed, it will never fail you.
FileInputStream fptr = new FileInputStream(new File("C:/Files/ABC.txt"));
ABC.txt is the file name to be read from. If locally defined in another function and passed as parameter to the FSM above, then the FSM function header would look like below.
public static double readValue(FileInputStream fptr) throws FileNotFoundException, IOException
Although not very neatly designed, it will never fail you.
public static double readValue() throws FileNotFoundException, IOException {
String myString = "";
String chString = "0123456789.";
int state = 1;
double value;
do
{
return value;
}
fptr is the handle of the text file you would be reading data from. It is a globally defined variable of type FileInputStream with definition given below.String myString = "";
String chString = "0123456789.";
int state = 1;
double value;
do
{
char buffer = (char)fptr.read();
if(chString.indexOf(buffer) == -1)
{
{
}
if(chString.indexOf(buffer) >= 0 && (state == 1) | (state == 2) | (state == 3))
{
} while(true);if(chString.indexOf(buffer) == -1)
{
if((state == 1) | (state == 2))
state = 2;
if(state == 3)state = 2;
{
value = Double.parseDouble(myString);
break;
}break;
}
if(chString.indexOf(buffer) >= 0 && (state == 1) | (state == 2) | (state == 3))
{
myString = (new StringBuilder()).append(myString).append(buffer).toString();
state = 3;
}state = 3;
return value;
}
FileInputStream fptr = new FileInputStream(new File("C:/Files/ABC.txt"));
ABC.txt is the file name to be read from. If locally defined in another function and passed as parameter to the FSM above, then the FSM function header would look like below.
public static double readValue(FileInputStream fptr) throws FileNotFoundException, IOException

Comment