I am learning how to factor my codes so I thought it would be nice to create a function solely for retrieving a char from the input file. This is what I have in mind:
public char getChar( String infile )
{
try
{
BufferedReader in = new BufferedReader( new FileReader( infile ));
int ch = in.read();
// do some decision making
return (char)ch;
}
catch( IOException e )
{
System.out.println( e.getMessage() );
System.exit(1);
}
}
Then in the constructor or some another function I can use it like this:
public constructor( String infile )
{
char newChar = getChar( infile );
// some lines of codes later.. need another character
newChar = getChar( infile );
}
P.S. I haven’t test these codes so it might contain so errors, but i hope my idea is understandable.
Please tell me if this is a good/bad idea or this kind of factoring can be done in different ways. Thank you for reading.
EDIT: Yes, I do want the BufferedReader to get the next char in line.. and not start over
Ex: infile contains a String like this: “ABC”
thisShouldBeA = getChar( infile );
thisSoundBeB = getChar( inflie );
1 Answer