Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);
   }
     }

DemoEx.java

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);
    }
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote