Generate all possible numbers which can be representated as the sum of 2 cubes below N I have written a program that generates all possible numbers which can be represented by adding 2 cubes which have value less than N

For example: when \$N = 10, 1^3 + 2^3 = 9\$

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AllNumberLessThanNasCubes {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("enter an integer n");
        int n = new Scanner(System.in).nextInt();

        System.out.println("all possible numbers are as follows ");
        generateAllPossibleNumbers(n);

    }

    /**
     * This function will generate all possible numbers less the {@link n} which can 
     * be represented as the sum of 2 cubes
     * For eg.when N=10 ,  1^3 + 2^3 = 9 
     * @param n
     */
    public static  void generateAllPossibleNumbers(int n) {
            // TODO Auto-generated method stub

    List&lt;Long&gt; cubes = new ArrayList&lt;&gt;((int) Math.cbrt(n));
    List&lt;Long&gt; allSum = new ArrayList&lt;&gt;();
    for (int i=1; Math.pow(i, 3)&lt; n ;++i){
        cubes.add((long) Math.pow(i, 3));
    }
    long sum=0 , tempSum=0;
    int cubesSize= cubes.size();
    System.out.println(cubes);
    for (int i=0; i &lt; cubesSize ;++i){
        sum = cubes.get(i);

        for (int j=i+1; j&lt; cubesSize ; ++j){
            tempSum = sum + cubes.get(j) ;
            //System.out.println(tempSum);
            if (tempSum &gt;= n){
                break;
            }else{
                allSum.add(tempSum);
            }
        }
    }
    System.out.println(allSum);
}
}


Is this the correct way to solve the problem? Is there an efficient way to solve it?

Except for error checking while taking input, what else can be improved?
Except for error checking


Error checking would be nice, and can easily be done, so why not add it to the top of your method:

    if (n &lt; 0) {
        throw new IllegalArgumentException("number must not be negative");
    }



  what else can be improved?



your formatting, especially your spacing, which seems very random, and thus makes the code hard to read. Just use an IDE to fix this for you.
define variables in as small a scope as possible. For example, sum can be declared inside the loop as long sum = cubes.get(i);.
don't print in your method, but return the values instead. This makes the next point a lot easier, and makes your method reusable in general.
write unit tests to verify the functionality of your code, and to make it a lot easier to make changes without being afraid of breaking the code.