# Encoding show text

**URL:** https://discourse.processing.org/t/encoding-show-text/34414
**Category:** Coding Questions
**Tags:** homework
**Created:** [January 1, 2022, 5:47pm UTC](https://discourse.processing.org/t/encoding-show-text/34414 "2022-01-01T17:47:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kristina](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kristina](https://discourse.processing.org/u/Kristina)
#### Post date: [January 1, 2022, 5:47pm UTC](https://discourse.processing.org/t/encoding-show-text/34414/1 "2022-01-01T17:47:40Z")

</div>

Hello! Ive been working on this code we got for homework, we can’t change anything about it but we can just add in between two comments.

Its supposed to count the letters and then show the result as a text underneath for example  
AABBAAA would be 2xA 2xB 3xA

I think I got it to count the letters but the text isn’t showing up. I don’t know what im supposed to do without changing the rest of the code. If someone could help me with this that would be really nice 🙂  
Just an idea of what I have to do or change.

> // Run-Length Encoding Exercise  
> // Please add your code to the computeRunLengthEncoding function  
> // It is strictly forbidden to change any other line of code.  
> char letters; // Input array for random letters  
> // Open window and initialize letter array  
> void setup() {  
> size(1000, 150);  
> fillArray();  
> }  
> // Initializes the letter array with a  
> // random length between 10 and 20 and with a  
> // random sequence of ‘A’ and ‘B’ letters  
> void fillArray() {  
> int sizeOfInputString = (int)random(10, 21);  
> letters = new char[sizeOfInputString];  
> for (int i=0; i\<letters.length; i++)  
> letters[i] = random(0, 1)\<0.5 ? ‘A’ : ‘B’;  
> }  
> // Whenever a key is pressed, the input array  
> // becomes reinitialized to test more input configurations.  
> void keyPressed() {  
> fillArray();  
> }  
> // Method for reading the global letter array and computing  
> // an output string that represents a run-length encoded version  
> // of the input data. The output string is returned.  
> // Example input : AAABBBBAA  
> // Desired output : 3xA , 4xB , 2xA  
> String computeRunLengthEncoding() {  
> String result = “”;  
> // Start to add your code below this line  
> int count = 1;  
> for (int i = 0; i \< letters.length; i++) {  
> if (i \< letters.length - 1 && letters[i] == letters[i + 1]) {  
> count++;  
> } else {  
> System.out.println(count);  
> System.out.println(letters[i]);  
> count=1;  
> }  
> }  
> // Add your code above this line  
> return result;  
> }  
> // Main drawing method that displays input and output  
> void draw() {  
> background(0, 0, 150);  
> textSize(40);  
> textAlign(CENTER);  
> // Draw input string:  
> text(new String(letters), width/2, 60);  
> textSize(20);  
> // Draw run-length encoded string:  
> text(computeRunLengthEncoding(), width/2, 100);  
> }

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 1, 2022, 9:14pm UTC](https://discourse.processing.org/t/encoding-show-text/34414/2 "2022-01-01T21:14:26Z")

</div>

> [@Kristina](#):
>
> String computeRunLengthEncoding()

You work within this function

This function returns a value that gets displayed in draw() automatically (throughout) using the text() command in the last line

You need to set the value of the returned variable within the function - copy your findings into this variable

Hint: it’s a String

---

<div class="post-metadata">

### Author: ![Kristina](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kristina](https://discourse.processing.org/u/Kristina)
#### Post date: [January 11, 2022, 4:38pm UTC](https://discourse.processing.org/t/encoding-show-text/34414/3 "2022-01-11T16:38:13Z")

</div>

I thought I have to write something like  
result = new String();

and then put the count etc in there. But no matter how I do it, it only gives me like 2 letters max.  
Am I on the right Track though? I feel like its a really easy solution but I can’t seem to find out what I have to do 🙂

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 11, 2022, 5:12pm UTC](https://discourse.processing.org/t/encoding-show-text/34414/4 "2022-01-11T17:12:50Z")

</div>

often we see a function like void setup(). It returns nothing (void).

Your function is:

```
String computeRunLengthEncoding()

```

It returns a variable of type String.

This return is done in this line:

```
 return result;

```

The result is then used in draw() here:

```
text(computeRunLengthEncoding(), width/2, 100);

```

So, we need to put the value of result correctly (we do this in the function) so it is displayed in `draw()`.

So, yes, you are on the right track.

**What you have to do**

result is already defined in the function here: `String result = "";` (no need to say new String!)

So what you have to do is something like

Where you say

```auto
System.out.println(count);
System.out.println(letters[i]);

```

add / replace with:

`result = result + str(count) + " " + letters[i] + "; ";`

Thus you put the desired content into `result`.

I hope this helps.

Chrisir

---

<div class="post-metadata">

### Author: ![Kristina](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kristina](https://discourse.processing.org/u/Kristina)
#### Post date: [January 11, 2022, 5:34pm UTC](https://discourse.processing.org/t/encoding-show-text/34414/6 "2022-01-11T17:34:57Z")

</div>

Thank you it works now! I made a mistake first but I figured it out. I placed it wrongly. Thank you for your help 🙂
