从末尾填充数组

时间:2013-03-12 01:42:05

标签: java arrays double

我有一个双重拆分成一个int数组,但我遇到的问题是,我需要它从后面填写,像这样:

    int[] arrayA = new int[5];
    int[] arrayB = new int[5];
    x = 23456.08;
    //code here

    //what im left with:
    arrayA [0,2,3,4,5,6];
    arrayB [0,8];

这是我如何切断双人的:

      public static void main(System args[])
{
    Scanner input = new Scanner(System.in);
    String answer = "";
    int count = 0;
    int thatone = 0;
    int[] splitD = new int[5]; //main num
    int[] splitDec = new int[1]; //decimal
    //Enter the Number
    System.out.print("Enter a number to convert: ");
    double num = input.nextDouble();

           // convert number to String
    String convert = num + "";
    // split the number
    String[] split = convert.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]);
        count++;
    }

    // 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]);
        }
    }
    for(int i =0; i<count;i++)
    {
        if(i ==0) // x00000.00 or 000x00.00
        {
            if(splitD[0] != "0")
            {
                switch (splitD[i])
                {
                    case 9: answer+="Nine"; break;
                    case 8: answer+="Eight"; break;
                    case 7: answer+="Seven"; break;
                    case 6: answer+="Six"; break;
                    case 5: answer+="Five"; break;
                    case 4: answer+="Four"; break;
                    case 3: answer+="Three"; break;
                    case 2: answer+="Two"; break;
                    case 1: answer+="One"; break;
                    default: answer+=""; break;
                }
                answer+= " Hundred ";
            }
            else
            {
                answer+= "";
            }
        }
        else if(i ==1)//this goes with i =2 //0x0000
        {
            if(splitD[i] == 1)
            {
                switch (splitD[i+1])
                {
                    case 9: answer+="Nineteen"; break;
                    case 8: answer+="Eighteen"; break;
                    case 7: answer+="Seventeen"; break;
                    case 6: answer+="Sixteen"; break;
                    case 5: answer+="Fifteen"; break;
                    case 4: answer+="Fourteen"; break;
                    case 3: answer+="Thirteen"; break;
                    case 2: answer+="Twelve"; break;
                    case 1: answer+="ten"; break;
                    default: answer+=""; break;
                }
                answer+= " Thousand ";
                thatone = 1;
            }
            else
            {
                switch (splitD[i])
                {
                    case 9: answer+="Ninety"; break;
                    case 8: answer+="Eighty"; break;
                    case 7: answer+="Seventy"; break;
                    case 6: answer+="Sixty"; break;
                    case 5: answer+="Fifty"; break;
                    case 4: answer+="Fourty"; break;
                    case 3: answer+="Thirty"; break;
                    case 2: answer+="Twenty"; break;
                    case 1: answer+=""; break;
                    default: answer+=""; break;
                }
            }
        }
        else if(i == 2) //00x000
        {
            if(thatone ==0)
            {
                switch (splitD[i])
                {
                    case 9: answer+=" Nine"; break;
                    case 8: answer+=" Eight"; break;
                    case 7: answer+=" Seven"; break;
                    case 6: answer+=" Six"; break;
                    case 5: answer+=" Five"; break;
                    case 4: answer+=" Four"; break;
                    case 3: answer+=" Three"; break;
                    case 2: answer+=" Two"; break;
                    case 1: answer+=" One"; break;
                    default: answer+=""; break;
                }
                answer+= " Thousand ";
            }
            else
            {
            }


        }
        else if(i ==3)
        {
             switch (splitD[i])
            {
                case 9: answer+="Nine"; break;
                case 8: answer+="Eight"; break;
                case 7: answer+="Seven"; break;
                case 6: answer+="Six"; break;
                case 5: answer+="Five"; break;
                case 4: answer+="Four"; break;
                case 3: answer+="Three"; break;
                case 2: answer+="Two"; break;
                case 1: answer+="One"; break;
                default: answer+=""; break;
            }
            answer+= " Hundred ";
        }
        else if(i ==4) //0000x0
        {
            switch (splitD[i])
            {
                case 9: answer+="Ninety"; break;
                case 8: answer+="Eighty"; break;
                case 7: answer+="Seventy"; break;
                case 6: answer+="Sixty"; break;
                case 5: answer+="Fifty"; break;
                case 4: answer+="Fourty"; break;
                case 3: answer+="Thirdy"; break;
                case 2: answer+="Twenty"; break;
                case 1: answer+=""; break;
                default: answer+=""; break;
            }
            answer+= " ";
        }
        else if(i ==5) //00000x
        {
            switch (splitD[i])
                {
                    case 9: answer+="Nine"; break;
                    case 8: answer+="Eight"; break;
                    case 7: answer+="Seven"; break;
                    case 6: answer+="Six"; break;
                    case 5: answer+="Five"; break;
                    case 4: answer+="Four"; break;
                    case 3: answer+="Three"; break;
                    case 2: answer+="Two"; break;
                    case 1: answer+="One"; break;
                    default: answer+=""; break;
                }
        }
}
       if(splitDec[0] == 0)
        {
           answer += " and 00/100 Dollars";
       }
       else if(splitDec[1] == 0)
       {
           answer += " and " +splitDec[0] + "0/100 Dollars";
       }
               else
       {
           answer += " and " +splitDec[0] +splitDec[1] +" /100 Dollars";
       }
                System.out.println(answer);
    }
    }

如何让数组在适当的位置添加0? 例如,如果我输入54.00,我会得到:

    int[] SplitD = {0,0,0,0,5,4};

谢谢!

2 个答案:

答案 0 :(得分:0)

考虑为每个字符创建一个String数组,一个String(包括小数点),这将是一个更好的方法:

String[] chars = String.valueOf(num).split("(?<=.)");

完成工作。

编辑:

要使用开关,请不要拆分:

for (byte b : String.valueOf(num).getBytes()) {
    switch(b) {
    case '.':
    case '1':
    }
}

答案 1 :(得分:0)

而不是重新初始化数组:

// 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]);
    count++;
}

使用现有的(长度= 5),从后面迭代:

if(charArray1.length > 5) // heavy hard-code, would prefer something better
    throw new IllegalArgumentException("Maximum 5 digits, on both decimal sides.");
    // watch out of ArrayIndexOutOfBoundsException

for (int i = charArray1.length - 1, j = splitD.length -1 ;
                                    i >=0 && j >=0; i--, j--) 
{
    // convert char to int
    splitD[j] = Character.getNumericValue(charArray1[i]);
    count++;
}

回想一下,int数组在初始化时用零填充。