0

Having successfully created a policy holder and writing the information to csv I try to run the program again using a do while loop but receive an inputmismatchexception after typing surname.

Can someone point me in the right direction or advise what I am doing wrong with this piece of code?

Here is example of what happens... console

/**
 * 
 */
package InsurancePolicySystem;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @author
 *
 */
public class InsurancePolicy {

    // create variables
    private String surname;
    private int age;
    private String motorType;
    private String motorPolicyId;
    private String addAnother;
    private String pattern = "^[a-zA-Z](\\s?[a-zA-Z]){2,29}$";

    // import scanner for user input
    Scanner scanner = new Scanner(System.in);

    // time stamp number. -1 used on month as time stamp displays next year instead
    // of current year
    int year = Calendar.getInstance().get(Calendar.YEAR);
    int month = Calendar.getInstance().get(Calendar.MONTH) - 1;

    // sum of the year and month
    int timeStamp = year + month;

    /**
     * default constructor
     */
    public InsurancePolicy() {

    }

    /**
     * non default constructor
     * 
     * @param surname
     * @param age
     * @param motorType
     * @param motorPolicyId
     */
    public InsurancePolicy(String surname, int age, String motorType, String motorPolicyId) {
        this.surname = surname;
        this.age = age;
        this.motorType = motorType;
        this.motorPolicyId = motorPolicyId;

    }

    /**
     * 
     * @return
     */
    public String getSurname() {
        return surname;
    }

    /**
     * Surname should be more than 3 letters and not greater than 20. Anything more
     * will cause a not valid error!
     * 
     * @param surname
     * @throws IllegalArgumentException
     */
    public void setSurname(String surname) throws IllegalArgumentException {
        this.surname = surname;
    }

    /**
     * 
     * @return
     */
    public int getAge() {
        return age;
    }

    /**
     * Age must be between 18 & 50 Any other age will produce not eligible error!
     * 
     * @param age
     * @throws IllegalArgumentException
     */
    public void setAge(int age) throws IllegalArgumentException {
        if ((age >= 18) && (age <= 50)) {
            this.age = age;
        } else {
            throw new IllegalArgumentException("Not eligible for an insurance policy!");
        }
    }

    /**
     * 
     * @return
     */
    public String getMotorType() {
        return motorType;
    }

    /**
     * motor type should only be either CAR, TAXI or BUS. if statement to prevent
     * other vehicle types and throw error if user inputs a different vehicle.
     * 
     * @param motorType
     * @throws IllegalArgumentException
     */
    public void setMotorType(String motorType) throws IllegalArgumentException {
        if (motorType.equals("CAR") || motorType.equals("TAXI") || motorType.equals("BUS")) {
            this.motorType = motorType;
        } else {
            throw new IllegalArgumentException("Not eligible for an insurance policy!");
        }
        this.motorType = motorType;

    }

    /**
     * 
     * @return
     */
    public String getMotorPolicyId() {
        return motorPolicyId;
    }

    /**
     * 
     * @param motorPolicyId
     */
    public void setMotorPolicyId(String motorPolicyId) {
        this.motorPolicyId = motorPolicyId;
    }

    /**
     * Method to write policy holder particulars to a csv file. Creates ArrayList
     * and uses collect joining to add comma for file.
     * 
     * @throws IOException
     */
    public void writeToFile() throws IOException {
        BufferedWriter writer = new BufferedWriter(
                new FileWriter("/Users/johnj/eclipse-workspace/InsuranceProject/insurance.csv", true));

        // creates policy holder (insurance) array
        ArrayList<String> insurance = new ArrayList<String>();
        insurance.add(surname);
        insurance.add(Integer.toString(age));
        insurance.add(motorType);
        insurance.add(motorPolicyId);

        // write array to csv file
        String collect = insurance.stream().collect(Collectors.joining(", "));
        System.out.println(collect);
        writer.write(collect);
        writer.newLine();
        writer.flush();
        writer.close();
    }

    /**
     * Builds and generates user input. Will then add it into an array list and
     * append to writeToFile(). Included do while loop for user to continue creating
     * policy profiles without having to re run program.
     * 
     * @param motorType
     */
    public void addPolicyHolder() throws IOException {

        try {

            do {

                // enter policy holder surname. Checks pattern to ensure compliance
                System.out.println("Please type your surname...");
                surname = scanner.next(pattern).toUpperCase().trim();
                setSurname(this.surname);

                // user input age
                System.out.println("Please type your age...");
                age = scanner.nextInt();
                setAge(this.age);

                // user input motor type which is converted to upper case
                System.out.println("Please type your motor type i.e. CAR, TAXI or BUS...");
                motorType = scanner.next().toUpperCase().trim();
                setMotorType(this.motorType);

                System.out.println(); // used to create next line space

                // Motor Policy Id should be 3 characters from surname as well as time stamp. If
                // time stamp odd number add 1 or else add 0 if even.
                motorPolicyId = surname.substring(0, 3) + timeStamp;
                if (timeStamp % 2 == 0) {
                    motorPolicyId = this.motorPolicyId + 0;
                } else {
                    motorPolicyId = this.motorPolicyId + 1;
                }

                // creates csv file
                writeToFile();

                // prints completed user input of policy holder
                System.out.println();
                System.out.println("Surname      :" + this.surname);
                System.out.println("Age          :" + this.age);
                System.out.println("Policy Ref   :" + this.motorPolicyId);
                System.out.println("Motor Type:  :" + this.motorType);

                System.out.println(); // used to create next line space

                // asks user if they would like to create a different policy holder
                System.out.println("Add another Policy Holder (Y or N)..");
                addAnother = scanner.next();
            } while (addAnother.equalsIgnoreCase("Y")); // If N program ends.
        } catch (Exception exception) { // if exception program ends
            System.out.println(
                    "There is a problem ... Policy Holder does meet necessary criteria, run the program again.");
        } finally {
            scanner.close();
            System.out.println("Program ended!"); // end of program
        }

    }

public static void main(String[] args) throws IOException {

        InsurancePolicy insurancePolicy = new InsurancePolicy();
        insurancePolicy.addPolicyHolder();

    }

}

I am able to complete a first run but do while loop fails when trying to add another policy holder.

12
  • 1
    What was the value you provided? Since you are using next(pattern) are you sure it matches String pattern = "^[a-zA-Z](\\s?[a-zA-Z]){2,29}$";? Commented Feb 9, 2023 at 18:11
  • 1
    Also note that next can only read values until delimiter of Scanner, which by default is set to one or more whitespace. This is why next can (by default) only return single words which don't contain whitespaces, so pattern which matches those words will never match \\s. Commented Feb 9, 2023 at 18:15
  • 1
    "..I try to run the program again using a do while loop but receive an inputmismatchexception after typing surname." your console example doesn't let us see that problem. To get better help please consider reducing your example to SSCCE (a.k.a. minimal reproducible example) - in other words get rid of things which are not related to problem you are showing (this will also help you to narrow its cause which may lead you to solving it yourself). Also make it runnable/debuggable for us - maybe add to it main method which we could run and get exact error you are facing. Commented Feb 9, 2023 at 18:35
  • 1
    Off-Topic: One poor practice in your code is printing a generic message when an exception is thrown: (Exception exception) { System.out.println( "There is a problem ... Policy Holder does meet necessary criteria, run the program again.");. In debugging, it helps to display the actual exception.toString. Commented Feb 9, 2023 at 19:11
  • 1
    Also, another good practice is to not depend on the exception throwing behavior of Scanner, and instead use the hasNext... methods (e.g., hasNext, hasNextInt, etc.) which you can use in if statements to determine if the next input meets the criteria. Commented Feb 9, 2023 at 19:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.