2

I have a code snippet (see snippet below) that generates an array like this: [0, 3, 1, -2, 0, -1, 1, 1, -2]. The int numbers here represent movements from one position to another. I would like to have the numerical values translated into text that would represent directions starting from the 0. A Positive number represents the number of steps to the East--so the number 3 would be translated into "eee", the number two would be "ee" and so on. Negative values represents steps in the opposite direct West so that -2 would be displayed as ww, and so on. No movement should be represented as 0.

I'm pretty new to all this and am not sure how the take the values from the array and turn them into the instructions as described above.

The code below shows how the array of integers is generated--subtracting the next location from the previous to get the number of steps between them.

int [] differenceX = new int [noOfRecordsX];
differenceX [0] = 0;

for( int i=0; i < noOfRecordsX -1 ;i++)
{
   differenceX [i+1]= inputX [i+1] - inputX[i];
}


From here I want to generate the text describing the steps in the respective direction so that this array:

[0, 3, 1, -2, 0, -1, 1, 1, -2]

would be transformed to this string:

0,eee,e,ww,0,w,e,e,ww

Gangnus
  • 24,044
  • 16
  • 90
  • 149
AngryCubeDev
  • 155
  • 2
  • 16
  • Is "differenceX" the array you are referring to that generating the output "[0, 3, 1, -2, 0, -1, 1, 1, -2]"? In other words you want to translate "[0, 3, 1, -2, 0, -1, 1, 1, -2]" into a string to look like "0,eee,e,ww,0,w,e,e,ww"? – Barns Dec 30 '18 at 01:57
  • As I understood, the source array contains POSITIONS, and we have to output the MOVEMENTS between them. Barns understood that you already have movements in the array and want to transfer them into strings. Explain it better. – Gangnus Dec 30 '18 at 11:15
  • @Gangnus :: Looks good to me!! – Barns Dec 30 '18 at 23:38
  • @Barns After my edition, it does. It is a conceptual error, to describe input values in the task by their types. They should be defined, not by representation (in real it is not the business of the task setter), but by their ***roles*** – Gangnus Dec 30 '18 at 23:45
  • The array generator is erroneous - it will never fill the differenceX[0] – Gangnus Dec 30 '18 at 23:47

3 Answers3

1

You can do so by using the following code :

int arr[] = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };

for (int i = 0; i < arr.length; i++) {
    if (arr[i] < 0) { // west
        for (int j = arr[i]; j < 0; j++) {
            System.out.println("w");
        }
    } else if (arr[i] > 0) { // east
        for (int j = 0; j < arr[i]; j++) {
            System.out.println("e");
        }
    }
}
  • If the number is negative then we iterate from that value upto 0.
  • If the number is positive then we iterate from 0 upto that value.
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • Thanks for the answer, works well. How would I change the print and place all the directions into a textView? – AngryCubeDev Dec 29 '18 at 13:47
  • Not sure of that, but maybe this [link](https://stackoverflow.com/questions/22374117/how-to-create-simple-android-textview-and-display-text-on-it-using-java-code) could help you out. – Nicholas K Dec 29 '18 at 13:50
1

If you wish to get the string back instead of just writing to the console try this:

private void testMyMethod(){
    String resultString = "";
    int[] array = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };

    for(int step : array){
        String direction = convertToDirection(step);
        // Adding a comma -- as you requested
        // just add this in case you what to indicate a start point ==> X
        if(direction.isEmpty()){
            resultString = resultString.concat("X");
        }
        else{
            resultString = resultString.concat(direction);
        }
        resultString = resultString.concat(",");
    }
    resultString = resultString.subString(0, resultString.length()-1);
    myTextView.setText(resultString);
}

private String convertToDirection(int step){
    String direction = "";

    if(step > 0){
        direction = "e";
    }
    else if(step < 0){
        direction = "w";
    }

    String result = "";
    int len = Math.abs(step);

    for(int i = 0; i < len; i++){
        result = result.concat(direction);
    }

    return result;
}




Edit:
A less verbose solution:

private void testMyMethod(){
    int[] array = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };
    StringBuilder sb = new StringBuilder();
    for(int step : array){
        sb.append(convertToDirection(step).concat(","));
    }
    // Remove the last ","
    sb.deleteCharAt(sb.length()-1);
    myTextView.setText(sb.toString());
}

private String convertToDirection(int step){
    if(step == 0) return "0";
    String direction = step > 0 ? "w" : "e";

    int len = Math.abs(step);
    return new String(new char[len]).replace("\0", direction);
}


Borrowing this: new String(new char[len]).replace("\0", direction);
from this solution: Repeat String

Barns
  • 4,850
  • 3
  • 17
  • 31
  • Thanks for the answer it works great. How would I split each of the results up with a comma so like `eee, e, ww, w, e, e, ww`? – AngryCubeDev Dec 29 '18 at 20:55
  • Just add a comma at the end of the String returned from `convertToDirection()` or in the `for` loop of the `testMyMethod()`. – Barns Dec 29 '18 at 21:13
  • Where do you count the difference? – Gangnus Dec 30 '18 at 00:49
  • 1
    @Gangnus :: I don't understand? What difference? The OP stated that they only wanted the `array` translated into a string. – Barns Dec 30 '18 at 01:13
  • As I understood, the source array contains POSITIONS, and we have to output the MOVEMENTS between them. So, you have to subtract. I will ask the asker for explanation. – Gangnus Dec 30 '18 at 11:13
  • @Gangnus I got the answer from Barnes. I was able to add in the a 0 for where there is no direction and this will help at a later stage. His answer solved my problem well! – AngryCubeDev Dec 30 '18 at 12:09
  • Thanks AngryCubeDev! I'm clad I could help you with your issue. I just wanted to make sure that I really understood your issue and that the answer I provided solved the question the way you intended. I intentionally kept the solution as clear as possible, so that you could follow step-by-step. I could have reduced the answer to about eight lines of code, but it might not have been as clear. :: I am also trying to wrap my head around why someone down voted may answer (the only answer down voted!). I guess I will never find out. – Barns Dec 30 '18 at 16:22
  • @AngryCubeDev You must explain your question in its body. – Gangnus Dec 30 '18 at 19:49
  • @Gangnus I'm confused as to why you'd say this as Barnes understood it as well as Nickolas K and gave answers that solved my problem. I went with Barnes answer as it didn't just print to the console it wrote to a string. – AngryCubeDev Dec 30 '18 at 21:15
  • @AngryCubeDev :: I took the liberty of editing your question to change the statement as I understood it. Please make any (re)corrections as you deem appropriate. BTW:: Your "Jaunt" app looks pretty cool! – Barns Dec 30 '18 at 21:30
  • @AngryCubeDev can you simply answer the question: are the source int data POSITIONS or CHANGES of them? And please, put it in your post. SO is not for you only. It is for many other people that will have their own problem, similar to yours ... Or not? They won't be able to define it. – Gangnus Dec 30 '18 at 23:34
0

We should better use char repeating instead of loops. Look at Simple way to repeat a String in java for different ways.

int arr[] = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };
StringBuilder output = new StringBuilder();
for(int step : array){
    int length = Math.abs(step);
    if (step < 0) { // west
        output.append(new String(new char[length]).replace("\0", "w"));
    } 
    else if (step > 0) { // east
        output.append(new String(new char[length]).replace("\0", "e"));
    }
    else  output.append("0");
}

}

Gangnus
  • 24,044
  • 16
  • 90
  • 149