• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with CS homework please :)  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
package hw1.java;


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"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at hw1.java.Hw1Java.main(Hw1Java.java:17)"
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
at hw1.java.Hw1Java.main(Hw1Java.java:25)
Java Result: 1"
Something about dividing by zero? Anyway thanks for everyone's help.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!