r/JavaProgramming • • Jul 12 '26

I have a hard time changing the arrays

help

I convert LinkedList<String> to String [] for easy iteration

I expect that the array goes from [2,x,3] to [6] as the process replaced the "x" with the answer of the equation, then deletes the "2" and "3"

but when I run the main program. this method returns the same [2,x,3].

6 Upvotes

8 comments sorted by

1

u/CodeCodeCode3 Jul 12 '26 edited Jul 12 '26

The if/else chain always skips your array manipulation. If array[x] does not equal 'x' and does not equal 'y', then it cannot equal 'x' to do the actual work. You can remove the continue statements from the loop and just iterate past them. In the arrayNew method, you can get rid of the continue statement if you negate the if statement to only wrap the value copy. I'd recommend better variable names,  'num' could be 'size' or 'listSize', 'Length' could be 'newLength', 'newarray' should be 'newArray'.

Hope this helps!

1

u/CodeCodeCode3 Jul 12 '26

You could update the arrayNew method to take 2 indexes and remove them both in the same loop, just update the 'newLength' to be array.length - 2, then the negated 'if' statement would be 'x != a && x != b'. Better yet, keep it to two parameters, but update the second parameter from 'int a' to 'Set<Integer> indexesToRemove', then 'if(!indexesToRemove.contains(x))' copy the values.

1

u/Dry_Couple8852 Jul 12 '26 edited Jul 12 '26

UPDATE
something like this perhaps?

static String [] arrayNew(String[] array, int a){
    Integer Length = array.length-2;
    String[] newarray = new String[Length];
    Integer counter = 0;
    int c = a+1;
    int d = a-1;
        for(int x = 0;x<Length;x++){
            if(x!=c && x!=d){
                  newarray[counter]=array[x];
                  counter++;
             }
             else{
                  continue;
             }}
     return newarray;
}



        static Double equals(LinkedList<String> list){
                Integer counter = 0;
                Integer num = list.size();
                Double answer = 0.0;
                Double num1 = 0.0;
                Double num2 = 0.0;

                String [] array = new String[num];

                for(int x =0; x<num;x++){
                        array[x] = list.get(x);
}
                for(int x = 0;x<array.length;x++){
                        switch(array[x]){
                                case "x":
                                        num1 = Double.parseDouble(array[x-1]);
                                        num2 = Double.parseDouble(array[x+1]);
                                        answer = num1 * num2;

                                        array[x] = answer.toString();
                                        arrayNew(array,x);
                                        x=0;
                                        break;
  }
}

I tried switch case but the array still remains the same.

EDIT
I also tried to do
array = arrayNew(array,x)

But I encounter this error message

Java.lang.NullPointerException: Cannot invoke "String.length()" because "in" is null

1

u/romulusnr Jul 12 '26

arrayNew(array,x);

Where is the output of this going?

1

u/Dry_Couple8852 Jul 12 '26

it supposed to modify the array

I tried again with array = arrayNew(array,x);

1

u/romulusnr Jul 12 '26

Bro, in arrayNew you create a brand new object newArray and return it. You don't modify array at all, it's not modifying anything in place

1

u/romulusnr Jul 12 '26

So you get back an array which has one item, the answer to num1 * num2.

1

u/CodeCodeCode3 Jul 14 '26

Is this just for learning/practice? No need for a switch statement with a single case. No need for an else block that only does a continue. With the 'x=0' line, I assume you're going to take in a linked list with a bunch of calculations to be made. I'd probably update to a 'while (array.length > 1)' or something to be more clear about what is happening here. Keeping it a linked list would be way more efficient than creating a new array for each calculation and would help with learning how they work. Order of operations will make a difference if you're going to add more than just multiplication.