Jump to content

Could someone explain to me why this doesn't work? (JAVA)

CreepyPro

I'm supposed to 

Write a method named switchPairs that accepts an array of strings as a parameter and switches the order of pairs of values in the array. Your method should swap the order of the first two values, then switch the order of the next two, and so on. For example, if the array stores:

 

String[] a = {"a", "bb", "c", "ddd", "ee", "f", "g"};

switchPairs(a);

Your method should switch the first pair ("a", "bb"), the second pair ("c", "ddd") and the third pair ("ee", "f"), to yield this array:

{"bb", "a", "ddd", "c", "ee", "f", "g"}

If there are an odd number of values, the final element is not moved.

Please explain the logic on why this isn't working not just giving me an answer.

 

 

FYI; I'm doing this on codestepbystep.com

public void switchPairs(String[] a) {
	int len = a.length;
    if (len%2 !=0) {
        for (int i =0; i<len-1;i++){
            String hold = a[i];
            String hold2 = a[i+1];
            a[i] = hold2;
            a[i+1] = hold;
        }
    } else {
        for (int i =0; i<len-1;i++){
            String hold = a[i];
            String hold2 = a[i+1];
            a[i] = hold2;
            a[i+1] = hold;
    }
}
}
 

 

Link to comment
Share on other sites

Link to post
Share on other sites

In your loop, you use the length of the original array (arr) as counter, but the new array (arr2) is double the length. Just doing i = i +2 doesn't help. here is a working version with minal changes.

 

The for loop now uses the original array (arr) and only counts up by 1 and the index used to access the new array (arr2) is now doubled every time.

 

public static int[] split(int[] arr) {
    int[] arr2 = new int[arr.length * 2];
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] % 2 != 0) {
            arr2[i * 2] = arr[i] / 2 + 1;
            arr2[i * 2 + 1] = arr[i] / 2;
        } else {
            arr2[i * 2] = arr[i] / 2;
            arr2[i * 2 + 1] = arr[i] / 2;
        }
    }
    return arr2;
}

 

EDIT: Did you remove your original question now and posted a new one?

Gaming Rig: Ryzen 9 5950x | 2x16GB DDR4 3200MHz | XFX Reference 6800 XT | MSI Unify X570 | Corsair MP600 2TB, Samsung 850 Evo 500GB | bequiet 850W Straight Power 11

Server: Ryzen 5 3600 | 4x32GB DDR4 ECC 2400MHz | Asrock Rack X470D4U | Samsung EVO Plus 250GB, 6x Seagate Exos 8TB, Samsung 850 Pro 1TB | bequiet 550W Straight Power 11

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Jisagi said:

In your loop, you use the length of the original array (arr) as counter, but the new array (arr2) is double the length. Just doing i = i +2 doesn't help. here is a working version with minal changes.

 

The for loop now uses the original array (arr) and only counts up by 1 and the index used to access the new array (arr2) is now doubled every time.

 


public static int[] split(int[] arr) {
    int[] arr2 = new int[arr.length * 2];
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] % 2 != 0) {
            arr2[i * 2] = arr[i] / 2 + 1;
            arr2[i * 2 + 1] = arr[i] / 2;
        } else {
            arr2[i * 2] = arr[i] / 2;
            arr2[i * 2 + 1] = arr[i] / 2;
        }
    }
    return arr2;
}

 

yeah I fixed that my making a diff variable for arr2 but this is better thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, this one has the same kind of issue as before. Both for loops count upwards by 1, but you switch pairs on index i AND i+1. The solution is to increase the for loop counter by 2 each time and it should work.

Gaming Rig: Ryzen 9 5950x | 2x16GB DDR4 3200MHz | XFX Reference 6800 XT | MSI Unify X570 | Corsair MP600 2TB, Samsung 850 Evo 500GB | bequiet 850W Straight Power 11

Server: Ryzen 5 3600 | 4x32GB DDR4 ECC 2400MHz | Asrock Rack X470D4U | Samsung EVO Plus 250GB, 6x Seagate Exos 8TB, Samsung 850 Pro 1TB | bequiet 550W Straight Power 11

Link to comment
Share on other sites

Link to post
Share on other sites

Your logic is wrong because you didnt look at it as a mathematics problem. You made overly complex checks when you can just go through every second index. The answer is actually in the problem description: no change on the last index if there is an odd length. We don't have to check if the array length is even or odd. We just use the natural law of adding 2 to a number (starting from zero) always produces an even number, and we exit before we reach the final index.

public static String[] split(String[] arr) {
	for (int i = 0; i < arr.length - 1; i+=2) {
        var String temp = arr[i];
        arr[i] = arr[i+1];
        arr[i+1] = temp;
    }
    return arr;
}
Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, CreepyPro said:

lease explain the logic on why this isn't working not just giving me an answer.

Let's just take your first loop, both have the same issue :

 

 

for (int i =0; i<len-1;i++)
{
    String hold = a[i];
    String hold2 = a[i+1];
    a[i] = hold2;
    a[i+1] = hold;
}

 

This loop for "String[] a = {"a", "bb", "c", "ddd", "ee", "f", "g"};" will loop 7 times.

first go index is 0 and steps are :

- store index 0 string

- store index 1 string

- set index 0 with index 1 stored string (the famous swap)

- set index 1 with index 0 stored string(the famous swap)

 

Then it loop back and index increase to 1 which is the problem because :

- store index 1 string (currently contain the original index 0 value)

- store index 2 string (currently contain index 2 string has it hasn't changed yet)

- set index 1 with index 2 stored string (the famous swap)

- set index 2 with index 1 stored string(the famous swap but containing what was originally in index 0)

 

So by increasing this way all it effectively does is push back all values backward 1 index while pushing forward in the array the index 0 until it hit the end.

I highly suggest you get debugging skills. If you are unable to run logic in your head yet (don't worry it will eventually come) go with easier solution.

Start with simple thing like debug output inside the loop and output all the values. The best thing to do i really output in readable format everything you are manipulating.

 

- output something telling you what index you are about to work with

- output the full array with it's values at this moment

- output what i index number is and what it's value is

- output what i+1 index number is and what it's value is

- make your swap

- output again the full array with it's values at this moment so you have a difference.

 

With that you will see the pattern happening.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×