简体   繁体   English

如何在java中为拆分字符串分配多个对象?

[英]How do I assign multiple objects a part of a split string in java?

Here is my current code: 这是我目前的代码:

    public static void main(String[ ] args)
{
    CodeBreaker thisProgram = new CodeBreaker();

    String uncodedMessage = " 83 101 110 100 32 121 111 117 114 32 116 101 97 99 104 101 114 32 97 110 32 101 109 97 105 108 32 116 111 100 97 121";

    thisProgram.decoder(uncodedMessage);
}

public void decoder(String codedMessage){
    String[] parts = codedMessage.split(" ");
    int numberOfCharactersRemaining = parts.length;
    int count = 0;

    while (count <= numberOfCharactersRemaining) {
        String[] partsOf = codedMessage.split(" ");
        int n = 0; 
        System.out.print(partsOf[1 + n] + " ");
        n = n + 1;
        count = count + 1;

    }

}

This output the first character in the string whereas I want to have variables (part1, part 2 and so on) created and assigned with their respective part of the string, how do I do this? 这输出字符串中的第一个字符,而我想要创建变量(第1部分,第2部分等)并分配字符串各自的部分,我该怎么做?

The parts you are referring to are already there in your code, they are stored within the parts variable and can be accessed via parts[0], parts[1], ..., parts[x] with x being the length of the array. 您所指的部件已存在于您的代码中,它们存储在parts变量中,可以通过parts[0], parts[1], ..., parts[x]其中x是部件的长度。阵列。

I have made some changes to your code to output the correct results. 我对您的代码进行了一些更改以输出正确的结果。

public static void main(String[ ] args)
{
    CodeBreaker thisProgram = new CodeBreaker();
    String uncodedMessage = " 83 101 110 100 32 121 111 117 114 32 116 101 97 99 104 101 114 32 97 110 32 101 109 97 105 108 32 116 111 100 97 121";
    thisProgram.decoder(uncodedMessage);
}

public void decoder(String codedMessage)
{
    String[] parts = codedMessage.split(" ");
    int numberOfCharactersRemaining = parts.length;
    int count = 0;
    int n = 0; //This needs to be outside of the while loop so it doesn't reset to 0 every time.

    while (count < numberOfCharactersRemaining) //Removed equal comparison.
    {
        //Removed this since you have already split the code and stored it in the parts variable.
        //String[] partsOf = codedMessage.split(" "); 
        System.out.print(parts[n] + " "); //Removed the '+1' so it will not go out of bounds, changed also to parts[n] instead of partsOf[n].
        n = n + 1;
        count = count + 1;
    }
}

are you looking for something like this. 你在找这样的东西吗?

public static void decoder(String codedMessage) 
{
    String[] arr = codedMessage.trim().split(" ");
    for(int i =0;i<arr.length;i++)
        System.out.println(arr[i]);
}

Your question is a bit unclear, but I think this is what you are looking for: 你的问题有点不清楚,但我认为这就是你要找的东西:

public void decoder(String codedMessage) {
    String[] parts = codedMessage.split(" ");
    int numberOfCharactersRemaining = parts.length;
    int count = 0;

    while (count < numberOfCharactersRemaining) {
       System.out.println(parts[count]);
       count = count + 1;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM