-2

I do not know what is wrong. But my code will not run. And I do not know why. Thanks! The error was "Error: Could not find or load main class undefined" Because I was trying to answer this question from my java book:

The Harrison Group Life Insurance company computes annual policy premiums based on the age the customer turns in the current calendar year. The premium is computed by taking the decade of the customer’s age, adding 15 to it, and multiplying by 20. For example, a 34 year old would pay $360, which is calculated by adding the decades (3) to 15, and then multiplying by 20.

Write an application that prompts a user for the current year and a birth year. Pass both to a method that calculates and returns the premium amount, and then display the returned amount.

My code:

import java.util.Scanner;
class calculatePremium
{

    public static int calculatePremium(int currentYear, int birthYear)
    {
        int decade = currentYear - birthYear;
        decade = ((decade / 10) + 15) * 20;
        return decade;
    }

    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter current year : ");
        int currentYear = scan.nextInt();
        System.out.print("Enter birth year : ");
        int birthYear = scan.nextInt();
        int amt = calculatePremium(currentYear, birthYear);
        System.out.println("Premium amount is : $" + amt);
    }
}
Community
  • 1
  • 1
Doser D
  • 7
  • 2
  • Do you get any errors? What happens? – elixenide Nov 11 '17 at 04:29
  • Please, could you provide any compile-time error messages, this will be very helpful? – Tebogo Khanye Nov 11 '17 at 04:29
  • I have put in the post what the error was. And I tried looking up to understand what it meant. But I have been looking everywhere. And I just can not understand it – Doser D Nov 11 '17 at 04:31
  • I have tried reading posts and tried fixing my code. And I just can not fix it no matter how much I try. – Doser D Nov 11 '17 at 04:32
  • Scott the answer you have given did not work. It still gives me the same error – Doser D Nov 11 '17 at 04:37
  • Screenshot on how you are running it? – johnII Nov 11 '17 at 04:46
  • I copied this code in my eclipse and it works. – HaroldSer Nov 11 '17 at 04:59
  • In IDE's there is not any problem of setting the path, so it will work absolutely fine in any IDE . Problem arises when we write the code in notepad and run it through cmd or for that matter through terminal in linux. – anupam691997 Nov 11 '17 at 05:03
  • Are you running this code from IDE, cmd, notepad, linux terminal? if so provide the command. – HaroldSer Nov 11 '17 at 05:17
  • The problem is in running the code, not in compiling it, so you need to show how you run it. – Erwin Bolwidt Nov 11 '17 at 05:18
  • https://drive.google.com/open?id=1790tUUyADXOJvBjULB60eExVEfPjpo_x please see the link. I am running in linux. – anupam691997 Nov 11 '17 at 05:19
  • class name is not same as file saved – vikas kumar Nov 11 '17 at 05:34
  • 1
    You need to put all information *in your question* by using the **edit** link, as text (not as screenshot). In any case, I did look at your screenshot. And it shows that **you can run the program just fine**. So what are you asking here, really? https://drive.google.com/open?id=1z-6kg50yuPrBQf7OGZKAXxmq2Me_Ajkc – Erwin Bolwidt Nov 11 '17 at 06:18
  • I am running my program in cengage. – Doser D Nov 11 '17 at 06:35
  • @ErwinBolwidt, I have changed the link. drive.google.com/open?id=1790tUUyADXOJvBjULB60eExVEfPjpo_x I was just trying to tell that if we save the program with same name as that of class name it works just fine. – anupam691997 Nov 11 '17 at 06:57

3 Answers3

0

The error is coming because you might not have saved the program containing file with the same name as that of class name.

https://drive.google.com/open?id=1790tUUyADXOJvBjULB60eExVEfPjpo_x

Please see the images above, The name with which the file is saved and the name of the class and finally the correct execution of the code

anupam691997
  • 310
  • 2
  • 8
0

This error message:

"Error: Could not find or load main class undefined"

is really odd. Normally, it would give the name of some class (not "undefined"). In that form, it is a common beginner question with a standard answer:

The only way anyone can help you with your particular version of this problem is if you tell us clearly and precisely how you are compiling and running that code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

First thing to remember, always your main method containing class should be public and save your file name with public class name like calculatePremium.java in your case.

Mostch Romi
  • 531
  • 1
  • 6
  • 19