hi anyone
I am doing a car insurance program in java and i need to understand this error. the program goes like this. it depend on the driver's age and how many accidents he had. from 1 to 5 it have difference charges with additional surcharge an for 6 you dont qualify for the insurance.

am trying to implement an if statement to determine the drivers age but getting a error. it goes as this


the error that I am getting is: illegal start of a statement and also to create a class in numofaccidents in package carInsurance.
how can i fix this

I think there must be something else wrong beyond that small snippet. Can you post up the whole if statement?

Your variable might not be defined probably. But to fix it we need to see all of the code.

that is how far I reach in coding the program. i stop right there when I get the error and have been looking to see where the error is

There must be more code. Unless your class only contains that one line...
If you are unwilling to post up the rest of the code there is very little we can do to help you. Although I can say in java you will get that error if your if statement is malformed. Is there an else or if else that also makes up part of that complete if statement?

Also in java the code wouldn't even compile if it was only that one line. So please post the rest of your code or we cannot help you.

i am not unwilling but here is the code

double surcharge,  additional_surcharge, basic_insurance;
       int totalsurcharge;
       int driver_age, numofaccidents, add_surcharge, basic_surcharge = 0;
       String first_name, last_name, home_address ;

        System.out.println("please enter the first name");
        first_name = keyin.next();

        System.out.println("please enter last name");
        last_name = keyin.next();

        System.out.println("please enter the driver age");
        driver_age = keyin.nextInt();

        System.out.println("please enter your home address");
        home_address = keyin.nextLine();

        if(driver_age < 25){
            System.out.println("you have to pay a surcharge of: $100");
        else if(driver_age == 25){
            System.out.println("you have to pay insurance cost: $500 " + basic_surcharge);
            totalsurcharge = (500 + 100);
        System.out.println("how many accidents did you have");
        numofaccidents = keyin.nextInt();-here i am getting a red line saying to create field for keyin (why)


         System.out.println("the cost of insurance is: $500 " + "basic_surcharge");
         int totalsurcharge = (500 + 50);

          System.out.println("how many accident did you have?");
          numofaccidents = keyin.nextInt();
         else if(numofaccidents == 2){
          System.out.println("the surcharge cost is: $125 " + basic_surcharge + add_surcharge);
          totalsurcharge = (500 + 125 + 100);
          else if(numofaccidents == 3){
         System.out.println("the surcharge cost is: $225 " + basic_surcharge + add_surcharge);
         totalsurcharge =(500 + 100 + 225);
         else if(numofaccident == 4) {
         System.out.println("the cost of the surcharge is: $375 " + basic_surcharge + add_surcharge);
         totalsurcharge =(500 + 375 + 100);
        else if(numofaccidents == 5){
         System.out.println("the cost of surcharge is; $575 " + basic_surcharge + add_surcharge);
          totalsurchargg = (500 + 575 + 100);
         else if (numofaccidents > 6){
         System.out.println("you are not qualified for insurance");

for the if statement- if(numofaccidents == 1) it is telling to create a class for it in carInsurance package, why, I have create a variable for it

I have finished writing the code and now I have many errors.

totalsurcharge = (500 + 575 + 100); I have an error here telling me - class, enum expected, what does that mean and it is all the same for the rest of them

else if(numofaccidents == 2){- the error is class, interface, enum expected what does that mean

At line 28 you have a closing } that doesn't look like it needs to be there.
Same for line 39. You have two closing braces there. The second one is matched to anything. Which would explain the enum warning at the compiler is using that brace to close of your class or method, leaving everything after it hanging.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.