Question: ***Please add on/modify the existing code below with requirements #3 below*** 3: Create a method (called DataSave) that will write data to a file. This

***Please add on/modify the existing code below with requirements #3 below***

3: Create a method (called DataSave) that will write data to a file. This method will accept an array of Person objects.

The method will open the file (data.txt), and place each person object information (from the array) into the file (1 per line).

Data items you are putting into each line of the file are:

FName (string)

LName (string)

age (int)

married status (boolean).

** You may assume there are appropriate get methods for your use.

***current java code***

import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.InputMismatchException;

import java.util.Scanner;

public class Test {

public static void main(String[] args) throws FileNotFoundException {

int ar[] = new int[10]; // array to contain int value

// scanner to take input Scanner sc = new Scanner(new File("data.txt"));

int input = 0;

for (int i = 0; i < ar.length; i++) { // for length of array

try {

input = sc.nextInt();// reading values from text file line by line

if (input == 0) { // if input is zero

throw new IllegalValueException(); // Throw this exception

}

}

catch (IllegalValueException e) {

// filling array with 1 Arrays.fill(ar, 1);

System.out.println(e);// showing error

break;// breaking out of the loop

}

catch (InputMismatchException e) { // for input other then int

System.out.println("Input value mismatched"); // give warning and continue

Arrays.fill(ar, 0);// filling array with 0

System.out.println(e);// showing error

break;// breaking out of the loop

}

ar[i] = input; // set input to the array // for showing array values System.out.println("Array contains: " + Arrays.toString(ar)); } // for showing array values System.out.println("Array contains: " + Arrays.toString(ar)); sc.close();// closing the file

}

}

class IllegalValueException extends Exception {

IllegalValueException() {

super();

}

}

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!