Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am going through a Java book and drafting the examples and have run into the following error when compiling this code. Any help would be appreciated thank you.

Error:
Main.java:3: class Addition is public, should be declared in a file named 
Addition.java
public class Addition
       ^
1 error

Code:

import java.io.*;
import java.util.Scanner;
public class Addition    
{
  public static void main(String[] args)
  {
    java.util.Scanner input = new java.util.Scanner(System.in); 


    int number1;
    int number2;
    int sum;

    System.out.print( "Enter first digit: " );
    number1 = input.nextInt();

    System.out.print( "Enter second digit:" );
    number2 = input.nextInt();

    sum = number1 + number2;

    System.out.printf( "Sum is %d\n, sum" );    

  }
}
share|improve this question

closed as off topic by hhlp, Eliah Kagan, fossfreedom Jun 22 '12 at 19:55

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
This isn't really specific to Ubuntu. You might have better luck with programming questions on Stack Overflow. –  Ed Manet Jun 22 '12 at 18:31
    
Your question has nothing to do with Ubuntu.You should ask at stackoverflow.com ;) –  Nickolas Jun 22 '12 at 18:32

2 Answers 2

Source files in Java have to begin with a capitalized letter. Lowercases won't work.

Also, the public class Addition statement has to match the file name

Addition.java

import java.io.*;
import java.util.*;

public class Addition {
  //Addition code here
}

will work. However, if the file is named addition.java, it won't.

share|improve this answer

Your code will work if you name your source code file Addition.java.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.