Solution for
Parsing strings using split, Example 2
public static boolean fileVerifier(String fileName) {
try {
File inFile = new File(fileName);
Scanner in = new Scanner(inFile);
boolean isValid = true;
int lineNum = 0; // current line #
// read in each line in the file and check it for validity
while (in.hasNext()) {
String line = in.nextLine();
lineNum++;
isValid = validLine(line, lineNum) && isValid;
}
// post-condition: isValid is true only if each line in
// the file is valid
in.close(); // close the scanner (and file)
return isValid;
} catch (FileNotFoundException fnfe) {
System.out.println("File not found");
return false;
}
}
/**
* Private helper method used to check a single line for validity.
* Note that if a line contains more than one error, only the first error
* detected results in an error message (and the method then returns false,
* i.e., the rest of the line is not checked).
*
* @param line the line to check
* @param lineNum the line # (used when printing error messages)
*/
private static boolean validLine(String line, int lineNum) {
// each line should contain 3 pieces of information (day, time, temp)
// separated by one or more spaces
String[] tokens = line.split("[ ]+");
// if there aren't exactly 3 tokens, there is an error
if (tokens.length != 3) {
System.out.println("line " + lineNum +
": # pieces of info incorrect");
return false;
}
// check day, time, and temp for correct format, printing out
// an error message if a format is incorrect
if (!validDay(tokens[0])) {
System.out.println("line " + lineNum + " : invalid day");
return false;
}
if (!validTime(tokens[1])) {
System.out.println("line " + lineNum + " : invalid time");
return false;
}
if (!validTemp(tokens[2])) {
System.out.println("line " + lineNum + " : invalid temp");
return false;
}
// if we've gotten here, everything was ok, so return true
return true;
}
/**
* Private helper method used to check a single day for validity.
* A string is a valid day if it is an integer between 1 and 366,
* inclusive.
*
* @param string the string to check
*/
private static boolean validDay(String str) {
try {
int day = Integer.parseInt(str);
return (1 <= day && day <= 366);
} catch (NumberFormatException nfe) {
return false;
}
}
/**
* Private helper method used to check a single time for validity.
* A string is a valid time if it is in the format "hh:mm:ss" where
* hh is between 00 and 23, and mm and ss are between 00 and 59
*
* @param string the string to check
*/
private static boolean validTime(String str) {
String[] tokens = str.split("[:]");
if (tokens.length != 3)
return false;
// to be valid, each token needs to be exactly 2 characters
if (tokens[0].length() != 2 || tokens[1].length() != 2
|| tokens[2].length() != 2)
return false;
// parse each token and check for appropriate range
try {
int hour = Integer.parseInt(tokens[0]);
if (hour < 0 || hour > 23)
return false;
int min = Integer.parseInt(tokens[1]);
if (min < 0 || min > 59)
return false;
int sec = Integer.parseInt(tokens[2]);
if (sec < 0 || sec > 59)
return false;
// if we've gotten here, everything was ok, so return true
return true;
// if any of the tokens are not integers, the time is invalid
} catch (NumberFormatException nfe) {
return false;
}
}
/**
* Private helper method used to check a single temperature for validity.
* A string is a valid temperature if it is a number between -127.8 and
* and 135.9, inclusive.
*
* @param string the string to check
*/
private static boolean validTemp(String str) {
try {
double temp = Double.parseDouble(str);
return (-127.8 <= temp && temp <= 135.9);
} catch (NumberFormatException nfe) {
return false;
}
}