Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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: class Addition is public, should be declared in a file named
public class Addition
1 error


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

    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 ;) – Rrj Jun 22 '12 at 18:32

Your code will work if you name your source code file

share|improve this answer

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

import java.util.*;

public class Addition {
  //Addition code here

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

share|improve this answer

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