将int数组的部分转换为整数?

时间:2015-02-09 00:34:23

标签: java arrays int

我试图将"22 12 20 28"形式的消息转换为数组,然后将[22, 12, 20, 28]转换为单个整数,比如22,12,20,然后是28。有代码应该工作,但由于某种原因,它没有。

public class Decrypt {
    private String myMessage;
    private String e = "2718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035402123407849819334321068170121005627880235193033224745015853904730419957777093503660416997329725088687696640355570716226844716256079882651787134195124665201030592123667719432527867539855894489697096409754591856956380236370162112047742722836489613422516445078182442352948636372141740238893441247963574370263755294448337";
    private int character;
    private int myInteger;
    private int eInteger;
    private String decryptedMessage = "";

    public Decrypt(String myMessage){
        this.myMessage = myMessage;
    }

    public String Decryption(){         
        String[] splitMessage = myMessage.split(" ");
        int[] numbers = new int[splitMessage.length];
        String[] eMessage = e.split("");
        int[] eNumbers = new int[eMessage.length];

        for (int i=0; i<myMessage.length(); i++){
            eInteger = eNumbers[i];
            myInteger = numbers[i];
            System.out.println(eInteger + " " + myInteger);
            character = myInteger - eInteger;
            String stringCharacter = Integer.toString(character);
            decryptedMessage = decryptedMessage + " " + stringCharacter;
            System.out.println(i);

        }
        return decryptedMessage;
    }

    public String toString(){
        return "Message: " + Decryption();
    }    
}

我认为问题在于:

for (int i=0; i<myMessage.length(); i++){
    eInteger = eNumbers[i];
    myInteger = numbers[i];
    System.out.println(eInteger + " " + myInteger);
    character = myInteger - eInteger;
    String stringCharacter = Integer.toString(character);
    decryptedMessage = decryptedMessage + " " + stringCharacter;
    System.out.println(i);    
}

但我不知道它有什么问题。我不需要帮助就可以更容易地做到这一点,我只是想知道为什么它没有返回我想要的东西。它刚刚回归0。

2 个答案:

答案 0 :(得分:0)

一个,

      int[] numbers = new int[splitMessage.length];

不会自动生成一个分配了所有转换为int的字符串的数组。你必须明确地这样做。

答案 1 :(得分:0)

您尚未编写任何将单个字符串转换为整数的代码。这通常可以通过以下方式完成:

for (int i = 0; i < numbers.length; i++) {
    numbers[i] = Integer.parseInt(splitMessage[i]);
}

如果您使用的是Java 8,还可以使用:

int[] numbers = Arrays.stream(splitMessage)
    .mapToInt(Integer::parseInt).toArray();