Question
***Please add on/modify the existing code below with requirements #2 and #3 below*** 1: Write java code to load an array. Use simple I/O (system.out.print,
***Please add on/modify the existing code below with requirements #2 and #3 below***
1: Write java code to load an array. Use simple I/O (system.out.print, etc.) to get integer values from the user and load the array (loop?).
Your code has the potential to generate 2 exceptions:
1) InputMismatch
2) IllegalValueException (assume this exists).
If the 1st occurs, change the input to be 99, warn the user and continue.
The 2nd exception will occur when the user enters a 0; if this occurs, notify the user that a 0 has been entered, and change the input to -99.
2: Take the code created in #2 above now load the array with data from a file.
The file is in the same directory as the program file and is called data.txt. Create the File and the Reader objects now you have to worry about a FileNotFoundException (just print the error message).
You must also adjust the handling of the InputMismatch (load the array with all 0s) and IllegalValueException (load the array with all 1s).
If the file was opened at all, ensure it is closed whether you had an exception or not. Write the code to load the data, then handle each catch block separately
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.util.InputMismatchException;
import java.util.Scanner;
public class LoadArray {
public static void main(String[] args) {
int ar[]=new int[100]; //array to contain int value
Scanner sc=null;
int input=0;
for(int i=0;i try{ sc=new Scanner(System.in); //scanner to take input input=sc.nextInt(); if(input==0){ //if input is zero throw new IllegalValueException(); //Throw this exception } } catch(IllegalValueException e){ input=-99; System.out.println(e); } catch(InputMismatchException e){ //for input other then int System.out.println("Input value mismatched"); //give warning and continue input=99; System.out.println(e); } ar[i]=input; //set input to the array } } } class IllegalValueException extends Exception{ IllegalValueException(){ super(); } } output 6 gf Input value mismatched java.util.InputMismatchException 5 0 IllegalValueException 6 4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started