Question: The code has a flaw that we want to fix. The text file we are reading is supposed to be filled with integers, one integer

The code has a flaw that we want to fix. The text file we are reading is supposed to be filled with integers, one integer per line. If one of the lines contains a corrupted piece of data (not an integer), the MultiCatch program stops, reports an error and exits. Change the flow of control in the sample code in such a way that a corrupted line (or multiple lines) of text file does not prevent the program to continue printing out the rest of the numbers from the file.

public class MultiCatch {

public static void main(String[] args) {

int number; // To hold a number from the file try

{

// Open the file.

File file = new File("Numbers.txt");

Scanner inputFile = new Scanner(file);

// Process the contents of the file.

while (inputFile.hasNext())

{ // Get a number from the file.

number = inputFile.nextInt();

// Display the number.

System.out.println(number);

} // Close the file.

inputFile.close();

}

catch(FileNotFoundException | InputMismatchException ex)

{

// Display an error message.

System.out.println("Error processing the file.");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!