What is the best way to read words from file knowing the line index? I want to read each word in the file and check if contains only digits. If it doesn't I want to print the word index and line index.
I'm currently using this, but I wonder if there is a way to do it without 2 loops.
private static void validateNumber ( String fileName )
{
try ( BufferedReader reader = new BufferedReader ( new FileReader ( fileName ) ) )
{
String line = null;
int lineIndex = -1;
while ( ( line = reader.readLine ( ) ) != null )
{
lineIndex++;
String [ ] words = line.trim ( ).split ( "[\\s]+" );
for ( int i = 0; i < words.length; i++ )
{
System.out.println("Word " + i " at line " + lineIndex);
}
}
} catch ( FileNotFoundException e )
{
e.printStackTrace ( );
} catch ( IOException e )
{
e.printStackTrace ( );
}
}