Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CAN SOMEONE HELP ME WITH THIS CODE? I HAVE TO PRINT OUT THE NUMBER OF SMOKER AND NUMBER OF NON SMOKER FROM A TEXT FILE.

CAN SOMEONE HELP ME WITH THIS CODE? I HAVE TO PRINT OUT THE NUMBER OF SMOKER AND NUMBER OF NON SMOKER FROM A TEXT FILE. CAN SOMEONE SHOW ME HOW IT CAN BE DONE?

//This is the demo for Policy Class import java.util.*; import java.io.*;

public class policy { public static void main(String[] args) throws IOException { //Create a instance of the File class File policyFile = new File("PolicyInformation.txt"); //Open File Text //Pass a reference to the File object as an argument to the Scanner class constructor Scanner inputFile = new Scanner(policyFile); //Declaring Variables int policyNumber = 0; String providerName = ""; String firstName = ""; String lastName = ""; int holderAge = 0; String smokingStatus = ""; double holderHeight = 0.0; double holderWeight = 0.0; double holderBmi = 0.0; double policyPrice = 0.0; String smoker = ""; final int SIZE = 0; int[] numSmoker = new int[SIZE]; int[] numNonsmoker = new int[SIZE]; //Create an array list to store objects. The ArrayList will hold Policy objects ArrayList policies = new ArrayList(); //While loop to read the file while(inputFile.hasNext()) { //Reads data from file policyNumber = inputFile.nextInt(); inputFile.nextLine(); providerName = inputFile.nextLine(); firstName = inputFile.nextLine(); lastName = inputFile.nextLine(); holderAge = inputFile.nextInt(); inputFile.nextLine(); smokingStatus = inputFile.nextLine(); holderHeight = inputFile.nextDouble(); inputFile.nextLine(); holderWeight = inputFile.nextDouble(); inputFile.nextLine(); //Create Policy Objects using the Policy Class type Policy p = new Policy(policyNumber, providerName, firstName, lastName, holderAge, smokingStatus, holderHeight, holderWeight); //Add Policy Objects to the ArrayList policies.add(p); }//end while loop //For Loop to display the following output for Policy Class for(int i = 0; i < policies.size(); i++) { //Displaying the following information for the Policy Class System.out.printf(" Policy Number: %.0f ", policies.get(i).getpolicyNumber()); System.out.println(" Provider Name: " + policies.get(i).getproviderName()); System.out.println("Policyholder's First Name: " + policies.get(i).getfirstName()); System.out.println("Policyholder's Last Name: " + policies.get(i).getlastName()); System.out.println("Policyholder's Age: " + policies.get(i).getholderAge()); System.out.println("Policyholder's Smoking Status(smoker/non-smoker): " + policies.get(i).getsmokingStatus()); System.out.println("Policyholder's Height: " + policies.get(i).getholderHeight() + " inches"); System.out.println("Policyholder's Weight: " + policies.get(i).getholderWeight() + " pounds"); System.out.printf("Policyholder's BMI: %.2f", policies.get(i).getholderBmi()); System.out.printf(" Policy Price: $%.2f", policies.get(i).getpolicyPrice()); //Blankline between the policies and numSmoker/numNonsmoker System.out.println(); }//end for loop //Close File inputFile.close(); //Display the number of Policyholders that are smokers and the number of Policyholders that are non-smokers. System.out.println(" The number of policies with a smoker: " + numSmoker.length); System.out.println("The number of policies with a nonsmoker: "); }//end main }//end class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions