Having some issues with my homework.
So i posted my code here yesterday and you all helped out alot, I really appreciate it. I am supposed to use integers for the user to enter an amount of bytes into the input, and it is supposed to output how many GBs, MBs, KBs, and bytes are in the amount of bytes that was entered. Here is my code so far but I am having a few errors.
I used the division and modulas like someone suggested (I figured that was how you were supposed to do it, and it runs and works for some inputs, say i entered 1073742849 bytes, then it will give me back 1 GB. but if enter a bigger number like 2500000000, it gives me this error.
"Exception in thread "main" java.util.InputMismatchException: For input string: "2500000000"
Also something is wrong with the integer variable "mbs" bc when I enter a number to check how it is working, for example i would enter 1049600 bytes then it will give me an error and return this.
"Exception in thread "main" java.lang.ArithmeticException: / by zero
Java Result: 1"
Something about dividing by zero? Anyway thanks for everyone's help.
The maximum value an int variable can hold is 2147483647. This is less than 2500000000.
If you need bigger values you should use long type (maximum value 9223372036854775807).
If you need values bigger that that you should use BigInteger.
You need to rethink how you calculate kbs, mbs etc.
Suppose you enter 0.
bytes = 0 bs = bytes = 0 kbs = bs / 1024 = 0 bs = bs % kbs = 0 % 0 And you have Exception in thread "main" java.lang.ArithmeticException: / by zero.
In case of 1049600 the situation is similiar. Somewhere you divide by 0. You need to remember that % means division (but it returns remainder instead of quotient).
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop