0

Im working on a program, where the user inputs a double, and I split the double up and put it into an array(Then I do some other stuff). The problem is, im not sure how to split up the double by digit, and put it into an int array. Please help?

Heres what im looking for:

    double x = 999999.99 //thats the max size of the double
    //I dont know how to code this part
    int[] splitD = {9,9,9,9,9,9}; //the number
    int[] splitDec = {9,9}; //the decimal
3
  • you could process it as a String Commented Mar 12, 2013 at 0:31
  • Missing ; in x declaration Commented Mar 12, 2013 at 0:31
  • I think I'd look at maybe BigDecimal. Commented Mar 12, 2013 at 0:32

3 Answers 3

2

You can convert the number to String then split the String based on . character.

For example:

public static void main(String[] args) {
        double x = 999999.99; // thats the max size of the double
        // I dont know how to code this part
        int[] splitD = { 9, 9, 9, 9, 9, 9 }; // the number
        int[] splitDec = { 9, 9 }; // the decimal

        // convert number to String
        String input = x + "";
        // split the number
        String[] split = input.split("\\.");

        String firstPart = split[0];
        char[] charArray1 = firstPart.toCharArray();
        // recreate the array with size equals firstPart length
        splitD = new int[charArray1.length];
        for (int i = 0; i < charArray1.length; i++) {
            // convert char to int
            splitD[i] = Character.getNumericValue(charArray1[i]);
        }

        // the decimal part
        if (split.length > 1) {
            String secondPart = split[1];
            char[] charArray2 = secondPart.toCharArray();
            splitDec = new int[charArray2.length];
            for (int i = 0; i < charArray2.length; i++) {
                // convert char to int
                splitDec[i] = Character.getNumericValue(charArray2[i]);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

There are several ways to do this. One is to first get the whole number part of the double and assign it to an int variable. Then you can use the / and % operators to get the digits of that int. (In fact, this would make a nifty function so you can reuse it for the next part.) If you know that you are only dealing with up to two decimal places, you can subtract the whole number part from the double to get the fractional part. Then multiply by 100 and get the digits as with the whole number part.

2 Comments

Wouldnt I have to know how long the number is to do that though? Its going to be a different number length most of the time.
@AlekMieczkowski The length of the whole number part isn't a problem. The length of the fractional part might be, though, if you don't know the maximum number of decimal places.
0

You could create a string from the double:

String stringRepresentation  = Double.toString(x);

Then split the string:

String[] parts = stringRepresentation.split("\\.");
String part1 = parts[0]; // 999999
String part2 = parts[1]; // 99

Then convert each of these to your arrays using something like:

int[] intArray = new int[part1.length()];

for (int i = 0; i < part1.length(); i++) {
    intArray[i] = Character.digit(part1.charAt(i), 10);
}

3 Comments

stringRepresentation.split("."); doesn't work. The argument to split is a regular expression pattern and in those patterns the "." is a metacharacter. An java.lang.ArrayIndexOutOfBoundsException is thrown in your code
I just tried it, and Iswanto San is correct. How would I go about to fixing this?
@AlekMieczkowski: try to fix the split parameter. use split("\\."); instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.