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
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