Question: Please Simple JAVA I/O exception question Please follow the bolded instruction of # of lines required etc TY class DataSetReader { private int[] data ;

Please Simple JAVA I/O exception question

Please follow the bolded instruction of # of lines required etc TY

class DataSetReader

{

private int[] data ;

/**

Reads the data from the file called filename and returns the

array of integers.

@param filename the name of the file from which to read.

@return the array of integers

*/

public int[] getData(String filename) throws IOException

{

Scanner scanner = new Scanner(new File(filename)) ;

try {

readData(scanner) ;

return data ;

}

finally {

System.out.println("Finally closing the scanner.") ;

scanner.close() ;

}

}

/**

Reads all data.

@param scanner the scanner that scans the data

*/

private void readData(Scanner scanner) throws BadDataException

{

String message = "The first token has to be a number indicating the number of values to be read." ;

//-----------Start below here. To do: approximate lines of code = 1

// 1. if there is not an integer coming up, throw a BadDataException with the above message.

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

int numberOfValues = scanner.nextInt() ;

data = new int[numberOfValues] ;

for (int i = 0 ; i < numberOfValues ; i++) {

readValue(scanner, i) ;

}

message = "End of file expected, but there are more values." ;

//-----------Start below here. To do: approximate lines of code = 1

// 2. if there is another token in the input stream, throw a BadDataException with the above message.

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

}

/**

Reads one data value

@param scanner the scanner that scans the data

@param i the position in the array for the next value

*/

private void readValue(Scanner scanner, int i) throws BadDataException

{

String message = "Data value expected, but find no integer." ;

//-----------Start below here. To do: approximate lines of code = 2

// 3. if there is no integer next in the file, throw a BadDataException with the message above;

//4. read the next integer into the array called data.

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

}

}

/**

This class reports bad input data.

*/

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!