If your purpose is just print , then you don't need to store and don't required String[] content
.. Just read the line and validate the line falls in particular pattern.
Move the condition to while loop.
while (inputStream.hasNext())
{
String content = inputStream.nextLine();
// Move the condition and check with content variable.
if ( content.matches("^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}\\:\\d{4}"))
{
System.out.println(content);
}
else
{
System.out.println("Not Valid");
}
}
In case your purpose is store the content . When you don't know the size / number of lines up ahead like this case , use List
to store which can grow dynamically as required.
Like
List<String> contents = new ArrayList<String>()
And add the valid line with in valid condition like this.
contents.add(content);