Question

how am I going to move the value of a char array to the same char array? Here's a code:

Assuming ctr_r1=1 ,

for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
 {
  letters[ctr_x] = letters[ctr_x];
 }

sb.append(letters);
char[] lettersr1 = sb.toString().toCharArray();
sb1.append(lettersr1);

append the "letters", then convert it to string, then convert it to char array then make it as "lettersr1" value.

what im trying to accomplish is given the word EUCHARIST, i need to take the word HARIST out and place it on another array and call it region 1 (Porter2 stemming algorithm).

The code "ctr_X = (ctr_r1 + 2)" starts with H until T. The problem is I cannot pass the value directly that's why i'm trying to update the existing char array then append it.

I tried doing this:

char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
 {
   lettersr1[ctr_x] = letters[ctr_x];
 }

sb.append(lettersr1);

but my app crashes when i do that. Any help please. Thanks!

Was it helpful?

Solution

I don't understand what you're trying to do, but I can comment on your code:

letters[ctr_x] = letters[ctr_x];

This is a noop: it sets an array element value to the value it already has.

char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++) {
    lettersr1[ctr_x] = letters[ctr_x];

This obviously causes a NullPointerException, since you're trying to access an array which is null. You must initialize the array before being able to modify it:

char[] lettersr1 = new char[someLength];

Additional note: you should choose better names for your variables. The names should tell what the variable represents, and they should respect the Java naming conventions (no underscores in variable names, camelCase). ctr_x, ctr_r1 and lettersr1 don't mean anything.

EDIT:

I'm still not sure what you want to do, and why you don't simply use substring(), but here's how to transform EUCHARIST to HARIST:

    char[] eucharist = "EUCHARIST".toCharArray();
    char[] harist = new char[6];
    System.arraycopy(eucharist, 3, harist, 0, 6);
    String haristAsString = new String(harist);
    System.out.println(haristAsString);

    // or

    char[] harist2 = new char[6];
    for (int i = 0; i < 6; i++) {
        harist2[i] = eucharist[i + 3];
    }
    String harist2AsString = new String(harist2);
    System.out.println(harist2AsString);

    // or

    String harist3AsString = "EUCHARIST".substring(3);
    char[] harist3 = harist3AsString.toCharArray();
    System.out.println(harist3AsString);

OTHER TIPS

May be so:

String str = "EUCHARIST";
str = str.substring(3);

and after toCharArray() or smth another

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top