Seems familiar.
Here is the complete program, suitable for copying and pasting to a source file, and then compiling and running. Play with the code for a while. Perhaps you can improve it somehow.
class MyDate
{
int day, month, year;
MyDate ( int d, int m, int y )
{day = d; month = m; year = y;}
public String toString()
{return day+"/"+month+"/"+year ;}
}
class FSADate
{
MyDate convert( String str )
{
int reject= 10; // rejection state
int state = 1; // the current state
char current; // the current character
int index = 0 ; // index of the current character
String buffer = null;
int day=0, month=0, year=0;
while ( index < str.length() && state != reject )
{
current = str.charAt( index++ ) ;
if ( state==1 && current >= '0' && current <= '9')
{
state = 2; buffer = "" + current ;
}
else if ( state==2 && current=='/' )
{
state = 4; day = Integer.parseInt( buffer );
}
else if ( state==2 && current >= '0' && current <= '9' )
{
state = 3; buffer += current ;
}
else if ( state==3 && current=='/' )
{
state = 4; day = Integer.parseInt( buffer );
}
else if ( state==4 && current >= '0' && current <= '9' )
{
state = 5; buffer = "" + current ;
}
else if ( state==5 && current=='/' )
{
state = 7; month = Integer.parseInt( buffer );
}
else if ( state==5 && current >= '0' && current <= '9' )
{
state = 6; buffer += current ;
}
else if ( state==6 && current=='/' )
{
state = 7; month = Integer.parseInt( buffer );
}
else if ( state==7 && current >= '0' && current <= '9' )
{
state = 8; buffer = "" + current;
}
else if ( state==8 && current >= '0' && current <= '9' )
{
state = 9; buffer += current ;
year = Integer.parseInt( buffer );
}
else
state = reject ;
}
if ( index == str.length() && state ==9 )
return new MyDate( day, month, year ) ;
else
return null ;
}
}
class DateTester
{
public static void main (String[] args)
{
String inString = null;
FSADate fst = new FSADate();
MyDate date = null;
if ( args.length == 1 )
inString = args[0];
else
{
System.out.println("java DateTester dd/mm/yy");
return;
}
if ( (date = fst.convert( inString )) != null )
System.out.println("The string is transformed to:" + date );
else
System.out.println("The string is rejected.");
}
}
(Java Review Question:) If you were writing this program professionally, you would not write an automaton from scratch. You would use a certain Java class that is very useful for breaking strings into pieces. What class is this?