Victor Northcraft
Victor Northcraft

Reputation: 1

Debugging a Java program - Byte Converter

Problem:

Write a program that converts a given number of Bytes into a more human readable format by converting it into one of the following: Bytes, Kilobytes, Megabytes, Gigabytes, or Terabytes. For example, 1024 Bytes would be 1.00 KB (KB = Kilobyte).

Below is my code that I've been trying to debug, but have not been successful thus far. I'm confused as to where I would be dividing by zero, given the error message below.

import java.text.DecimalFormat;
import java.util.Scanner;

class ByteConverter {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan1 = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("#.00");
        long input1;
        long conversionKilobyte;
        long conversionMegabyte;
        long conversionGigabyte;
        long conversionTerabyte;


        System.out.print("Enter number of bytes: ");
        input1 = scan1.nextLong();

        conversionKilobyte = input1 / 1024;
        conversionMegabyte = input1 / 1048576;
        conversionGigabyte = input1 / 1073741824;
        conversionTerabyte = input1 / (long).1099511627776;

        if (input1 > (long).1099511627775) {
            System.out.println(input1 + " " + "Bytes is" + " " + conversionTerabyte + " " + "TB");
        } else if (input1 >= 1073741824 && input1 <= (long).1099511627776) {
            System.out.println(input1 + " " + "Bytes is" + " " + conversionGigabyte + " " + "GB");
        } else if (input1 >= 1048576 && input1 <= 1073741823) {
            System.out.println(input1 + " " + "Bytes is" + " " + conversionMegabyte + " " + "MB");
        } else if (input1 >= 1024 && input1 <= 1048575) {
            System.out.println(input1 + " " + "Bytes is" + " " + conversionKilobyte + " " + "KB");
        } else {
            System.out.println(input1 + " " + "Bytes is" + " " + df.format(input1) + " " + "B");
        }

    }
}

This is the input and error message I'm receiving. Any help you be appreciated, Thank you!

(Input): Enter number of bytes: 5

(Error): Exception in thread "main" java.lang.ArithmeticException: / by zero at ByteConverter.main(ByteConverter.java:23

Upvotes: 0

Views: 99

Answers (2)

Martin Morek
Martin Morek

Reputation: 249

Try learn read error message:

(Error): Exception in thread "main" java.lang.ArithmeticException: / by zero at ByteConverter.main(ByteConverter.java:23

"23" means line number, so that error is at line 23, (long).1099511627776 has to be a zero

Upvotes: 0

rgettman
rgettman

Reputation: 178253

Using a decimal point is not helpful in converting 240 into a long. You've actually supplied a double literal, .1099511627776, which is less than 1, and you are casting it to a long, which yields 0.

Note that removing the decimal point is not enough, because 1099511627776 is not a valid int literal; the value is too large.

Use the L suffix to specify a long literal.

1099511627776L

Alternatively, shift 1L left 40 places.

1L << 40

Upvotes: 2

Related Questions