import java.util.Scanner; class datacheck { static public void main(String args[]) { Scanner console = new Scanner(System.in); int lineNum = 0; while (console.hasNextLine()) { // This is a so-called "try block with resources". It automatically // closes the resource when the try block exits. I have used continue // statements in this loop and the try block guarantees that the // lineTokenizer scanner will be closed even when a continue statement // is executed. try (Scanner lineTokenizer = new Scanner(console.nextLine())) { lineNum++; // determine if the line has a name field if (lineTokenizer.hasNext()) { lineTokenizer.next(); // consume the valid token } else { System.out.printf("Line %d: line must have the format 'name age singleness'\n", lineNum); continue; // proceed to the next line of input } // determine if the line has a second field, and if so, whether that // field is an integer if (lineTokenizer.hasNext()) { if (lineTokenizer.hasNextInt()) { lineTokenizer.nextInt(); // consume the valid token } else System.out.printf("line %d - %s: age should be an integer\n", lineNum, lineTokenizer.next()); } else { System.out.printf("line %d: must have fields for age and singleness\n", lineNum); continue; // proceed to the next line of input } // determine if the line has a third field, and if so, whether that // field is a boolean if (lineTokenizer.hasNext()) { if (lineTokenizer.hasNextBoolean()) lineTokenizer.nextBoolean(); // consume the valid token else { System.out.printf("line %d - %s: singleness should be a boolean\n", lineNum, lineTokenizer.next()); continue; // proceed to the next line of input } } else { System.out.printf("line %d: must have a field for singleness\n", lineNum); continue; // proceed to the next line of input } } } } }