Jump to content

Java errors

ToastFromDiscord

So, I'm trying to calculate the theoretical number of tweets possible in Java, but I keep getting the error "cannot convert type double to int" but as far as I know, there are no doubles in this code. Please help?

public class Main {

  public static void main(String[] args) {
   int x = 1;
   int y = 0;
   while(x<=280)
   {
     y = y+Math.pow(1114112,x);
     x=x+1;
   }
   System.out.println(y);
  }
}

Link to comment
Share on other sites

Link to post
Share on other sites

So I figured it out, but it overflows to all hell... What should I do to calculate it?

Link to comment
Share on other sites

Link to post
Share on other sites

Math.pow returns a double. Either cast to int or make y a double. Also with such large powers you are bound to overflow. Use BigInteger for this: https://stackoverflow.com/questions/11317875/finding-really-big-power-of-a-number

 

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, ian1467 said:

So, I'm trying to calculate the theoretical number of tweets possible in Java, but I keep getting the error "cannot convert type double to int" but as far as I know, there are no doubles in this code. Please help?

public class Main {

  public static void main(String[] args) {
   int x = 1;
   int y = 0;
   while(x<=280)
   {
     y = y+Math.pow(1114112,x);
     x=x+1;
   }
   System.out.println(y);
  }
}

Math.pow returns the data type "Double", you are trying to assign that value to the integer value y.

Intel i7 5820K (4.5 GHz) | MSI X99A MPower | 32 GB Kingston HyperX Fury 2666MHz | Asus RoG STRIX GTX 1080ti OC | Samsung 951 m.2 nVME 512GB | Crucial MX200 1000GB | Western Digital Caviar Black 2000GB | Noctua NH-D15 | Fractal Define R5 | Seasonic 860 Platinum | Logitech G910 | Sennheiser 599 | Blue Yeti | Logitech G502

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
Share on other sites

Link to post
Share on other sites

On 6/9/2018 at 11:10 AM, ian1467 said:

So, I'm trying to calculate the theoretical number of tweets possible in Java, but I keep getting the error "cannot convert type double to int" but as far as I know, there are no doubles in this code. Please help?


public class Main {

  public static void main(String[] args) {
   int x = 1;
   int y = 0;
   while(x<=280)
   {
     y = y+Math.pow(1114112,x);
     x=x+1;
   }
   System.out.println(y);
  }
}

 

Remember to use code tags next time to make it easier for us to read. 

My best guess is that this line is causing the problem:

 y = y+Math.pow(1114112,x);

as Math.pow returns a double data type. You'll can cast double to int but adding (int) in front or using the intValue() method.

 y = y + (int) Math.pow(1114112,x);
 y = y + (Math.pow(1114112,x)).intValue();

 

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

1114112280 is a truly gargantuan number (1694 decimal digits according to wolfram alpha), and is much larger than even 1114112279 (which "only" has 1688 digits), so the answer is 1.38 * 101693 (which is just 1114112280). If you really want to work it out exactly, there are no primitive datatypes in Java that can store numbers that big (double is limited to about 10308), but you can use BigInteger to calculate it (which has a .pow and a .add method).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×