将数字拆分为数组

时间:2016-03-17 13:22:41

标签: arrays

我在Java中这样做

我想分割整数的数字,

int Pin=125;

到数组,

int Pin_Extracted={Pin[0], Pin[1], Pin[2]};

代码:

import java.io.*;
class name {
    public static void main(String args[])throws IOException {
        InputStreamReader ir = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ir);
        int Pin;
        String Names[][] ={ {"Mr.","Mrs.","Miss.","Dr.","Sir","Late","Professor","Gadha","Master","Teacher"},{"Abhigyan","Akashdeep","Anish","Adarsh","Ashutosh","","Anik","Shivam","",""},{"Saha","Mukkherjee","Pandey","Shaw","Bannerjee","Dey","Gupta","Singh","",""} };        
        System.out.println("Enter your 3 digit code name :");
        Pin=Integer.parseInt(br.readLine());
        char cn[]=Pin.toCharArray();
        int cn1=cn[0], cn2= cn[1], cn3= cn[2];
        String Name1=Names[0][cn1], Name2=Names[1][cn2], Name3=Names[2][cn3];
        System.out.println(Name1+" "+Name2+" "+Name3);
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

public void int [] convertToArray(int pin){
     StringBuilder sB = new StringBuilder();
     sB.append(pin);
     String toConvert = sB.toString();
     int [] splittedValue = new int [toConvert.length];
     for(int i = 0; i < toConvert.length; i++){
        splittedValues[i] = Integer.parseInt(toConver.charAt(i));
    }
 return splittedValues; 
 } 

我认为这有效......问候

答案 1 :(得分:0)

您需要先将整数转换为字符串。

WAR

然后将新String作为一个字符数组进行迭代,然后将它们分配给一个新的int数组。

String pinStr = Integer.toString(Pin);