Please Simple JAVA I/O exception question Please follow the bolded instruction o
ID: 3727828 • Letter: P
Question
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.
*/
Explanation / Answer
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public 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
* @throws BadDataException
*/
public int[] getData(String filename) throws IOException, BadDataException
{
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.
if(scanner.hasNextInt()) {
throw new BadDataException();
}
//-----------------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.
if(scanner.hasNext())
throw new BadDataException();
//-----------------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;
if(scanner.hasNextInt()) {
throw new BadDataException(message);
}
//4. read the next integer into the array called data.
int numberOfValues = scanner.nextInt() ;
data = new int[numberOfValues] ;
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
}
/**
This class reports bad input data.
*/
class BadDataException extends Exception {
public BadDataException() {
}
public BadDataException(String msg) {
super(msg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.