• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Need help with CS homework please :)

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 2235
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
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
    Bookmark Topic Watch Topic
  • New Topic