1

I need to copy the string into a character array the largest number of times you can within the array. In this case OPERATIVO would enter 3 times in the char array and the rest of the array could be whatever character. That I can do?

public void copyToArray( char [] arrayCharacters ){
    String var = new String("OPERATIVO");

    for(int i=0 ; i < 30 ; i++){
        ...
    }
}
2
  • Do the size of arrays vary? Or would they vary? Commented Feb 24, 2014 at 17:50
  • Always has size 30 the arrayOriginal. Commented Feb 24, 2014 at 17:51

5 Answers 5

2

Not quite sure what you want, but I'll give it a shot:

Edit: try this one:

public static void copearCaracteres(char[] arrayOriginal )
{
    String cadena = new String("OPERATIVO");
    int div = arrayOriginal.length / cadena.length();
    int j = div * cadena.length();

    for(int i=0 ; i < arrayOriginal.length; i++)
    {
        if(i == j) return;
        arrayOriginal[i] = cadena.charAt(i % cadena.length());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, if only the last 3 should not be modified by not putting the whole word. Output: OPERATIVOOPERATIVOOPERATIVOXGB
1
arrayOriginal.length / cadena.length()

will give you the max number of times you can copy the String in the array.

In your case, 30/8 => 3

2 Comments

and as I copy to array 3 times?
@AngelSanchez Use % as Martin does in its answer and modify the end condition of your for loop : for(int i=0 ; i < arrayOriginal.length / cadena.length() * cadena.length(); i++)
1
    String cadena = new String("OPERATIVO");
    char[] cad = new char[30];
    int length = cadena.length();
    int count = 1;

    for(int i=0,j=0; i < 30 && j <= length ; i++ ){
        if(j==length)
        {
            j = 0;
            count++;
        }
        if(count > 3)
        {
            break;
        }
        cad[i] = cadena.charAt(j);
        j++;
    }

Output : OPERATIVOOPERATIVOOPERATIVO

5 Comments

if only the last 3 should not be modified by not putting the whole word. Output: OPERATIVOOPERATIVOOPERATIVOXGB
Probably that i < 30 should be arrayOriginal.length.
you mean to say the char size is 27 @AngelSanchez ??
No size is always 30. But only copy OPERATIVO while completely into the array.
@AngelSanchez ohkk gud
0

I don't know if i understand the question correctly, but if you want to copy the String to a character array then this is how you can do it:

public void copy(char[] arr) {
    String string = "Some String";
    if (arr.length < string.length())
        // error
    arr = string.toCharArray();
}

Comments

0

You can use java.lang.System.arraycopy():

String srcString = "OPERATIVO";
char[] srcArray = srcString.toCharArray();
int maxLength = 30;
char[] destArray = new char[maxLength];
int pos = 0;
while(pos < maxLength) {
  java.lang.System.arraycopy(srcArray, 
                             0, 
                             destArray, 
                             pos, 
                             Math.min(srcArray.length, maxLength - pos));
  pos += srcArray.length;
}
String string = new String(destArray);
System.out.println(string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.