Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Policy Class

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Policy Class

    Can someone help me figure out how to print the number of smoker and number of nonsmoker?


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

    //Create an array list to store objects. The ArrayList will hold Policy objects
    ArrayList<Policy> policies = new ArrayList<Policy>();

    //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("\nPolicy Number: %.0f ", policies.get(i).getpolicyNumber());
    System.out.println("\nProvider 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("\nPolicy 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("\nThe number of policies with a smoker: ");
    System.out.println("The number of policies with a nonsmoker: ");

    }//end main

    }//end class
    Attached Files Attached Files

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Policy Class

    What does the program output now? Please copy contents of console and paste it here so we can see what it is doing.
    Add some comments to describe what is wrong and show what is desired.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Policy Class

    [QUOTE=VanessaKitty021;164779]Can someone help me figure out how to print the number of smoker and number of nonsmoker?

    //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;
     
          //Create an array list to store objects. The ArrayList will hold Policy objects
          ArrayList<Policy> policies = new ArrayList<Policy>();
     
          //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("\nPolicy Number: %.0f ", policies.get(i).getpolicyNumber());
             System.out.println("\nProvider 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("\nPolicy 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("\nThe number of policies with a smoker: ");
          System.out.println("The number of policies with a nonsmoker: ");
     
       }//end main
     
    }//end class

    Output:

    Policy Number: 3450
    Provider Name: State Farm
    Policyholder's First Name: Alice
    Policyholder's Last Name: Jones
    Policyholder's Age: 20
    Policyholder's Smoking Status(smoker/non-smoker): smoker
    Policyholder's Height: 65.0 inches
    Policyholder's Weight: 110.0 pounds
    Policyholder's BMI: 18.30
    Policy Price: $550.00

    Policy Number: 3455
    Provider Name: Aetna
    Policyholder's First Name: Bob
    Policyholder's Last Name: Lee
    Policyholder's Age: 54
    Policyholder's Smoking Status(smoker/non-smoker): non-smoker
    Policyholder's Height: 72.0 inches
    Policyholder's Weight: 200.0 pounds
    Policyholder's BMI: 27.12
    Policy Price: $500.00

    Policy Number: 2450
    Provider Name: Met Life
    Policyholder's First Name: Chester
    Policyholder's Last Name: Williams
    Policyholder's Age: 40
    Policyholder's Smoking Status(smoker/non-smoker): smoker
    Policyholder's Height: 71.0 inches
    Policyholder's Weight: 300.0 pounds
    Policyholder's BMI: 41.84
    Policy Price: $641.85

    Policy Number: 3670
    Provider Name: Global
    Policyholder's First Name: Cindy
    Policyholder's Last Name: Smith
    Policyholder's Age: 55
    Policyholder's Smoking Status(smoker/non-smoker): non-smoker
    Policyholder's Height: 62.0 inches
    Policyholder's Weight: 140.0 pounds
    Policyholder's BMI: 25.60
    Policy Price: $500.00

    Policy Number: 1490
    Provider Name: Reliable
    Policyholder's First Name: Jenna
    Policyholder's Last Name: Lewis
    Policyholder's Age: 30
    Policyholder's Smoking Status(smoker/non-smoker): non-smoker
    Policyholder's Height: 60.0 inches
    Policyholder's Weight: 105.0 pounds
    Policyholder's BMI: 20.50
    Policy Price: $400.00

    Policy Number: 3477
    Provider Name: State Farm
    Policyholder's First Name: Craig
    Policyholder's Last Name: Duncan
    Policyholder's Age: 23
    Policyholder's Smoking Status(smoker/non-smoker): smoker
    Policyholder's Height: 66.0 inches
    Policyholder's Weight: 265.0 pounds
    Policyholder's BMI: 42.77
    Policy Price: $688.37

    The number of policies with a smoker:
    The number of policies with a nonsmoker:
    Last edited by Norm; February 18th, 2019 at 10:14 AM. Reason: Added / to ending code tag

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Policy Class

    Where are the comments in the output describing what is wrong with it
    and showing what the output should be?

    For example:
    Item 1: abc
    Results are 1234 <<<<<<< This is wrong, results should be 3456
    More output here


    The posted code does not compile without many errors.
    Please fix the compiler errors and post the correct code that will compile and execute.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Forum's policy on self promotion?
    By ChristopherLowe in forum The Cafe
    Replies: 9
    Last Post: August 1st, 2014, 10:32 AM
  2. IMPORTANT NOTICE: Academic Dishonesty Policy
    By helloworld922 in forum Paid Java Projects
    Replies: 0
    Last Post: December 3rd, 2012, 02:16 PM