import java.io. import java.util.*; public class DemoEx public static void main
ID: 3916196 • Letter: I
Question
import java.io. import java.util.*; public class DemoEx public static void main (String[] args) throws IOException f Scanner in- new Scanner (new FileReader ("HR. txt")); int badData 0 int goodData0; while (in.hasNext)) int valuein.nextInt(); if (value0) // voluntorily to throw a HRTLlegaValueException else // update the number of good data catch (InputMismatchException ex) t // consume the current bad data very important // hint: use next() to read a String // update the number of bad data catch (HRIllegalValueException ex) // update the number of bad data // print out the number of Good Data and Bad Data. public class HRIllegalValueException extends Exception // implement a construc tor that takes a String parameter // and prints out the corresponding message
Explanation / Answer
The required classes are given below in their respective tables with the file names mentioned in table header.
import java.io.*;
import java.util.*;
public class DemoEx{
public static void main(String[] args)throws IOException{
Scanner in = new Scanner(new FileReader("HR.txt"));
int badData = 0;
int goodData = 0;
while(in.hasNext()){
try{
int value = in.nextInt();
if(value < 0)
throw new HRIllegalValueException("Negative Value");
else
goodData++;
}
catch(InputMismatchException ex){
badData++;
in.next();
}
catch(HRIllegalValueException ex){
badData++;
}
}
System.out.println("The number of good data are: " + goodData);
System.out.println("The number of bad data are: " + badData);
}
}
public class HRIllegalValueException extends Exception{
public HRIllegalValueException(String msg){
super(msg);
}
}
import java.io.*;
import java.util.*;
public class DemoEx{
public static void main(String[] args)throws IOException{
Scanner in = new Scanner(new FileReader("HR.txt"));
int badData = 0;
int goodData = 0;
while(in.hasNext()){
try{
int value = in.nextInt();
if(value < 0)
throw new HRIllegalValueException("Negative Value");
else
goodData++;
}
catch(InputMismatchException ex){
badData++;
in.next();
}
catch(HRIllegalValueException ex){
badData++;
}
}
System.out.println("The number of good data are: " + goodData);
System.out.println("The number of bad data are: " + badData);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.