简体   繁体   中英

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?

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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