OK, here's a few things, but in general it's easier for people to help you if you state EXACT error messages rather than saying you got "some error":
- Use the normal conventions for class names: letters and numbers, starting the name with a captial letter. Don't try and put silly things like = signs inside class or variable names (the compiler won't let you!). So in this case, EMC2 could be a good name (and you need to call your file EMC2.java in that case)
- You've got a spurious brace { at the start of the class. If you format your code properly (see examples in books -- a common convention in Java is to put the opening brace at the END of the line), it will help you to spot this.
- The numbers you want to deal with clearly aren't chars. In fact there's not even such a thing as a Char (with capital letter) in Java. You need to use one of the standard Java data types suitable for numbers, int, long, float or double. In this case, I'd recommend 'double': this is the type to use for numbers that can be decimals, unless you've any reason to use something else. If you want to only use whole numbers, then use
long in this case, as int allows a maximum value of around 2 billion, and your multiplying by a large number.
- Be careful about CASE: if you declare a variable as "energy", it's no good later trying to refer to it as "Energy". (By the way, the convention in Java is that
class names have capitals, variables small letters.)
- Be careful about position of arguments in the call to printf() -- you had the parameter inside the quotes
- Call your variables useful things: if something represents "m" or "c", why not call it "m" or "c" rather than something meaningless like "number1" or "number2"?
So a corrected version could be:
Code:
public class EMC2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter m: ");
double m = input.nextDouble();
double c = 300000;
double cSquared = c * c;
double energy = m * cSquared;
System.out.printf("Energy is %f", energy);
}
}