-2

I am receiving the error "Possible lossy conversion from double to int" on the last line (error pointer pointing at the number "2"), when I am not attempting to convert anything from a double to an int. What am I missing?

System.out.println("This is the ancestry calculator. Enter number of generations: ");
Scanner input = new Scanner(System.in);
int generations = input.nextInt();
int ancestors;
ancestors = Math.pow (2, generations);
Charlie
  • 230
  • 1
  • 9

2 Answers2

1

Math.pow() method returns a double value so if you need to assign it to ancestors you need to cast it.

   ancestors =(int) Math.pow (2, generations); 
Harry Manoharan
  • 181
  • 1
  • 12
1

Use any of the following:

double ancestors = Math.pow (2.0, generations)

or

int ancestors = (int) Math.pow (2, generations)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110