Encoding show text

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 :slight_smile:
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);
}

1 Like

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

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 :slight_smile:

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

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

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

1 Like