Styles
Styles

Reputation: 25

Using Java for base conversions - help I think the summing of decimals or conversion part is off

I'm analyzing the contents of a data file with entries in the form: ABCDE, followed by the base BB. The goals are to process the file, convert each to decimal (base 10) and determine the sum of the decimal (base 10) values.

Reject any entry that is invalid, thus far I found 5 entries that are invalid.

But I think I am not either converting each to decimal base 10 correctly or summing these decimals properly. Currently my sum is: -587582260 which is incorrect!

Here is the full program:

//Class for converting a number with a base value to decimal from each line in a file
public class anyBaseToDecimal {
    // Main function
    public static void main(String[] args) throws Exception {
        // Declares line as string to store a line in file
        String lines, numString;
        // Declares 4 integer variables
        int base, power, total = 0, val, decimal;
        // Declare a character variable letter to store each character in numString
        char letter;
        // Opens a file in read mode
        FileReader file = new FileReader("BaseConversionInput.txt");
        BufferedReader br = new BufferedReader(file);
        // Gets each line till end of file is reached
        while ((lines = br.readLine()) != null) {
            // Splits each line into words using method split()
            String wordsArray[] = lines.split(" ");
            // Sets numString as first word in wordsArray
            numString = wordsArray[0];
            // Convert numString to uppercase
            numString = numString.toUpperCase();
            // Convert second word in array to integer and store it as base value
            base = Integer.parseInt(wordsArray[1]);
            // set decimal as 0 . It is used for storing decimal equivalent of number
            decimal = 0;
            // set power as 1
            power = 1;
            // Repeat the loop through each character in numString
            for (int i = numString.length() - 1; i >= 0; i--) {
                // Set letter as ith character of numString
                letter = numString.charAt(i);
                // If letter is a digit set value as letter-48. It is equivalent to its integer
                // value
                if (letter >= '0' && letter <= '9') {
                    val = letter - 48;
                } else {
                    // Set val as decimal equivalent of character ie A=10,B=11,C=12 etc
                    val = letter - 'A' + 10;
                }
                // If val is greater than or equal to base print invalid number and goto next
                // line
                if (val >= base) {
                    System.out.println("Not a valid number!");
                    break;
                }
                decimal += power * val;
                power = power * base;
                total += decimal;
            }
            // Display decimal equivalent of number
            System.out.println(decimal);
        }
        // display total
        System.out.println("total of decimal equivalent of numbers present in given file: " + total);
        // closes the file
        br.close();
    }
}

Contents of the data file consists of entries such as this:

k5Jjc0mAd 23

182152334a 13

42122320204 5

52310 7

99CG 18

2 19

Any help would be very much appreciated!

Upvotes: 0

Views: 95

Answers (1)

Lev M.
Lev M.

Reputation: 6269

You are summing incorrectly.

The line:

total += decimal;

Should be outside the for loop, otherwise you will keep adding partial sums, getting much bigger sum value than you should.

The reason you are getting a negative sum, is because your sum "rolls over" (goes above maximum integer size).

You should use long for total or perhaps even BigDecimal.

Upvotes: 1

Related Questions