user1915702
user1915702

Reputation: 35

Array How would you add a digit to the same location as its value?

The input comes as a String "543210". The code extracts each character using the charAt method and place them one after the other in a specific array location that corresponds to the value of the number. charAt(0) = 5 means that 5 should go intoarrayLocation 5. It doesnt seem to work. I even tried with arrayLists.

public class HugeInteger {

    private String digits;

    int[] arrayToStoreTheDigits = new int[6];

    public HugeInteger(String digits) {

        this.digits = digits;
        add();


    }

    public void add() {

        for (int i = 0; i < 5; i++) {
            arrayToStoreTheDigits[digits.charAt(i)] = digits.charAt(i);
            System.out.println(digits.charAt(i));
        }

    }

    public String toString() {

        return "" + arrayToStoreTheDigits + "/ " + digits.charAt(2);
    }

}

package Exercise8_17_HugeIntegers;

public class HugeIntegertester {
    // static HugeInteger huge;

    public static void main(String[] args) {

        HugeInteger huge = new HugeInteger("543210");

    System.out.println(huge.toString());

    }




}

Upvotes: 0

Views: 184

Answers (2)

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

Problem exists with your loop :

for (int i = 0; i < 5; i++) {  // condition shoule be i < 6
    // arrayToStoreTheDigits[digits.charAt(i)] = digits.charAt(i); // convert String to integer
    // shoule be
    arrayToStoreTheDigits[Integer.parseInt(digits.charAt(i))] = Integer.parseInt(digits.charAt(i));
    System.out.println(digits.charAt(i));
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500893

Your question is unclear, but I suspect the problem is here:

arrayToStoreTheDigits[digits.charAt(i)] = digits.charAt(i);

If digits.charAt(i) is '5' that has an integer value of 53, as that's the UTF-16 code unit for the character '5'. If you're trying to extract its value when viewed as a digit, you need to use Character.digit. Alternatively you could just subtract '0' if you really only care about 0-9, and are confident there will be no other characters.

So you could write your code like this:

char c = digits.charAt(i);
arrayToStoreTheDigits[c - '0'] = c;

Note that due to this initialization:

int[] arrayToStoreTheDigits = new int[6];

... your code will fail if it ever sees a value of '6' or greater.

Additionally, if you want to use all the characters in digits, your loop should be:

for (int i = 0; i < digits.length(); i++)

Overall this is a very odd thing to want to do - because the only values valid for array element 1 (for example) will be '1' (if the digit is present) or 0 (the default, if it's not). In particular, this loses all information about the position in which the digits occurred. If the class is meant to be similar to BigInteger, you should be writing something much more like this:

arrayToStoreTheDigits = new int[digits.length()];
for (int i = 0; i < arrayToStoreTheDigits.length; i++)
{
    // TODO: Digit validation
    arrayToStoreTheDigits[i] = digits.charAt(i) - '0';
}

So that after passing in "543210" you'd have an array of { 5, 4, 3, 2, 1, 0 }. That's now useful information.

Upvotes: 4

Related Questions