-1

I am making a multiple string input random swap without using a temp variable.

But when I input, this happens a few times:

I can't embed images yet...

This happens more frequently... (note that the first output is always null and some outputs occasionally repeat)

My code:

import java.util.Arrays;
import java.util.Scanner;

public class myFile {
    public static boolean contains(int[] array, int key) {  
     Arrays.sort(array);  
     return Arrays.binarySearch(array, key) >= 0;
    } 
    public static void println(Object line) {
        System.out.println(line);
    }
    public static void main(String[] args) {        
        Scanner in = new Scanner(System.in);

        String finalText = "";
        String[] input = new String[5];
        String[] swappedInput = new String[input.length];
        int[] usedIndex = new int[input.length];

        int swapCounter = input.length, useCounter;

        for (int inputCounter = 0; inputCounter < input.length; inputCounter++) {   //input
            println("Enter input 1 " + (inputCounter + 1) + ": ");
            input[inputCounter] = in.nextLine();
        }

        while (--swapCounter > 0) {
            do{
                useCounter = (int) Math.floor(Math.random() * input.length);
            }
            while (contains(usedIndex, useCounter));

            swappedInput[swapCounter] = input[swapCounter].concat("@" + input[useCounter]);
            swappedInput[useCounter] = swappedInput[swapCounter].split("@")[0];
            swappedInput[swapCounter] = swappedInput[swapCounter].split("@")[1];    

            usedIndex[useCounter] = useCounter;
        }

        for (int outputCounter = 0; outputCounter < input.length; outputCounter++) {
            finalText = finalText + swappedInput[outputCounter] + " ";
        }

        println("The swapped inputs are: " + finalText + ".");
    }
}
Bernhard Barker
  • 50,899
  • 13
  • 85
  • 122
teed
  • 13
  • 1
  • 3
  • 1
    All those image links should be posted as code formatted text. Please do this. – Hovercraft Full Of Eels Sep 03 '17 at 14:12
  • `usedIndex` should be replaced by `input` perhaps while calling contains method? or you haven't defined values in `usedIndex`? – SMA Sep 03 '17 at 14:17
  • Since I haven't defined values in `usedIndex`, then the input can swap freely. And why should I replace `usedIndex` by `input`, The (intended) purpose of `usedIndex` along with the `do-while` is to limit the input from repeating itself like `alice bob bob bob eddy carol`, instead of `alice bob dave eddy carol` – teed Sep 03 '17 at 14:26
  • `int swapCounter = input.length, useCounter;` what's this? – Naman Sep 03 '17 at 14:28
  • Well its like this code in with temp variable with `i` as the swapCounter and `j` as `useCounter` in javascript. `var i = memory_array.length, j, temp; while (--i > 0) {j = Math.floor(Math.random() * (i + 1)); temp = memory_array[j]; memory_array[j] = memory_array[i]; memory_array[i] = temp;}` – teed Sep 03 '17 at 14:32
  • [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) [How to debug small programs.](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Bernhard Barker Sep 03 '17 at 16:11

1 Answers1

1

Because of randomality some times useCounter is the same as swapCounter and now look at those lines (assume useCounter and swapCounter are the same)

        swappedInput[swapCounter] = input[swapCounter].concat("@" + input[useCounter]);
        swappedInput[useCounter] = swappedInput[swapCounter].split("@")[0];
        swappedInput[swapCounter] = swappedInput[swapCounter].split("@")[1];

In the second line you are changing the value of xxx@www to be www so in the third line when doing split you dont get an array with two values you get an empty result thats why exception is thrown in addition you should not use swappedInput because it beats the pourpuse (if i understand correctly yoush shoud not use temp values while you are using addition array which is worse) the correct sollution is to only use input array here is the solution

public class myFile {
public static boolean contains(int[] array, int key) {
    Arrays.sort(array);
    return Arrays.binarySearch(array, key) >= 0;
}

public static void println(Object line) {
    System.out.println(line);
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    String finalText = "";
    String[] input = new String[5];
    int[] usedIndex = new int[input.length];

    int swapCounter = input.length, useCounter;

    for (int inputCounter = 0; inputCounter < input.length; inputCounter++) {   //input
        println("Enter input 1 " + (inputCounter + 1) + ": ");
        input[inputCounter] = in.nextLine();
    }

    while (--swapCounter >= 0) {
        do {
            useCounter = (int) Math.floor(Math.random() * input.length);
        }
        while (contains(usedIndex, useCounter));

        // Skip if results are the same
        if (useCounter == swapCounter) {
            swapCounter++;
            continue;
        }
        input[swapCounter] = input[swapCounter].concat("@" + input[useCounter]);
        input[useCounter] = input[swapCounter].split("@")[0];
        input[swapCounter] = input[swapCounter].split("@")[1];

        usedIndex[useCounter] = useCounter;
    }

    for (int outputCounter = 0; outputCounter < input.length; outputCounter++) {
        finalText = finalText + input[outputCounter] + " ";
    }

    println("The swapped inputs are: " + finalText + ".");
}

}

urag
  • 1,048
  • 6
  • 23
  • It works some times because useCounter is chosen randomly – urag Sep 03 '17 at 14:41
  • But I still get a `null` on the first one – teed Sep 03 '17 at 14:49
  • Because you should switch from while (--swapCounter > 0) to while (--swapCounter >= 0) i changed it in the answer – urag Sep 03 '17 at 14:54
  • What about the non-repeating output, how to fix it since either the `do-while`, the `contains()` function is not working, or some other reason? – teed Sep 03 '17 at 15:01
  • I added the needed changes in the answer both in code and in the text – urag Sep 03 '17 at 15:18