Need help to solve (skyscraper)

please format code with </> button * homework policy * asking questions

String storey = " “;
String[] tokens=split(storey,”,");
int[] level ={0,1,2,3,4,5,6,7,8,9,10};
int j = 0;

void setup() {
size(1000,1000);
background (0);
}

void draw(){
textSize(45);

String caret ="";
if (millis() % 1000<500){
caret="|";
}
fill(255);
rect (0,0,1000,100);
fill(0);
text( storey + caret, 0, 60);

for (int i = 0; i<level.length; i++){
fill(211,211,211);
rect(i - 10, 900, 200, 100);

}
}

void keyTyped(){
if (key == BACKSPACE) {
if (storey.length()>0){
storey = storey.substring(0, storey.length()-1);
}
}
else if (key == ENTER) {
// We split when the user presses enter
int[] tokens = int(storey.split(","));
}
else {
storey += key;
}
}

Hi,

Welcome to the forum! :wink:

Please use the </> button to format your code on the forum or put it inside quotes ``` here ```.
You can also press Ctrl+T in the Processing editor before pasting it to auto indent your code!

When you post a question on the forum, try not to put the question in the title but rather describe it in the body of the message : what do you want this code to do? What did you try, what doesn’t work? Do you have a screenshot?

Then it’s going to help us answer your questions!

3 Likes

I agree.

  • What is happening now and what do you want to happen instead?

You have to state this in your post to get good answers.

Explanation

Need help to solve why the rect appear even when I haven’t do any input

setup() is running once, draw() is running on and on.

The rectangle (which one? The one on top or on the bottom? I assume you mean the bottom one) is displayed because you display it in the for-loop. The for-loop is i<level.length which is initially 11 (the length of the array level) OR did you mean i<tokens.length? Maybe you confused the two. That could be one reason.

Remark

Also, in keyTyped() you have int[] tokens = . Here I have to say when you repeat int[] it’s a new variable known only locally and NOT the String[] tokens you declare before setup(). Is that your intention?

Chrisir

Sketch

I modified the code only minimal, you can see now what the for-loop is doing.

String storey = " ";
String[] tokens=split(storey, ",");
int[] level ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int j = 0;

void setup() {
  size(1000, 1000);
  background (0);
}

void draw() {
  textSize(45);

  String caret ="";
  if (millis() % 1000<500) {
    caret="|";
  }
  fill(255);
  rect (0, 0, 1000, 100);
  fill(0);
  text( storey + caret, 0, 60);

  for (int i = 0; i<level.length; i++) {
    fill(211, 211, 211);
    rect(i * 40+30, 900, 33, 33);
  }
}

void keyTyped() {
  if (key == BACKSPACE) {
    if (storey.length()>0) {
      storey = storey.substring(0, storey.length()-1);
    }
  } else if (key == ENTER) {
    // We split when the user presses enter
    int[] tokens = int(storey.split(","));
  } else {
    storey += key;
  }
}

I now modified your Sketch more and you can submit Words from storey to tokens


String storey = "";
String[] tokens= {};  //  split(storey, ",");
//int[] level ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//int j = 0;

void setup() {
  size(1000, 1000);
  background (0);

  //  println(level.length);
}

void draw() {
  background (0);

  textSize(45);

  String caret ="";
  if (millis() % 1000<500) {
    caret="|";
  }
  fill(255);
  rect (0, 0, 1000, 100);
  fill(0);
  text( storey + caret, 0, 60);

  textSize(13);
  fill(255); 
  text("Please enter your Word. Submit with Enter, You can use Backspace. ", 
    200, 130); 

  for (int i = 0; i<tokens.length; i++) {
    fill(211, 211, 211);
    rect(i * 70+15, 900, 
      67, 33);
    textSize(13);
    fill(0); 
    text(tokens[i], 
      i * 70+15+3, 900+20);
  }
}

void keyTyped() {
  if (key == BACKSPACE) {
    if (storey.length()>0) {
      storey = storey.substring(0, storey.length()-1);
    }
  } else if (key == ENTER) {
    // We split when the user presses enter
    tokens =  (String[]) append (tokens, storey);
    // reset 
    storey="";
  } else {
    storey += key;
  }
}

Thank you for taking time to solve my problem. But what I would like to do is when I press 1, one rectangle will pop out and when I press 2, still one rectangle(another rectangle0 but 2x times the height. Like 3,4,5 and so on.

Ok noted. Thanks for the tips.

I see.

Okay, you can store how many numbers have been entered (variable int howManyNumbers=0; ).

Use this for the for-loop (as upper bound: i<howManyNumbers)

Make tokens int[] and global (define before setup())

On Enter: store the storey into tokens at pos j and say j++ then: tokens[j] = storey; j++;

When displaying the rects in the for loop use tokens[i] as the height of the rect

   rect(i * 70+15, 900-tokens[i] * 20, 
      67, tokens[i] * 20);

Chrisir

1 Like

Thanks for the tips. I will work on it.

1 Like

Sorry to trouble but I cannot solve it. Would you mind adding here?
String storey = “”;
String[] tokens= {}; // split(storey, “,”);
//int[] level ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//int j = 0;

void setup() {
size(1000, 1000);
background (0);

// println(level.length);
}

void draw() {
background (0);

textSize(25);

String caret ="";
if (millis() % 1000<500) {
caret="|";
}
fill(255);
rect (0, 0, 1000, 40);
fill(0);
text( storey + caret, 0, 30);

textSize(15);
fill(255);
text("Please enter your Word. Submit with Enter, You can use Backspace. ",
250, 60);

for (int i = 0; i<tokens.length; i++) {
fill(211, 211, 211);
rect(i * 70+15, 900,
200, 100);
textSize(15);
fill(0);
text(tokens[i],
i * 70+15+3, 900+20);
}
}

void keyTyped() {
if (key == BACKSPACE) {
if (storey.length()>0) {
storey = storey.substring(0, storey.length()-1);
}
} else if (key == ENTER) {
// We split when the user presses enter
tokens = (String[]) append (tokens, storey);
// reset
storey="";
} else {
storey += key;
}
}

I edited my post above.

Can you solve it now?

You haven’t changed anything. What have you tried?



String storey = "";
int[] tokens= {}; // split(storey, “,”);

void setup() {
  size(1000, 1000);
  background (0);
}

void draw() {
  background (0);

  textSize(25);

  String caret ="";
  if (millis() % 1000<500) {
    caret="|";
  }
  fill(255);
  rect (0, 0, 1000, 40);
  fill(0);
  text( storey + caret, 0, 30);

  textSize(15);
  fill(255);
  text("Please enter your number. Submit with Enter, You can use Backspace. ", 
    250, 60);

  for (int i = 0; i<tokens.length; i++) {
    fill(211, 211, 211);
    rect(i * 70+15, 900-tokens[i]*30, 
      200, tokens[i]*30);
  }
}

void keyTyped() {
  if (key == BACKSPACE) {
    if (storey.length()>0) {
      storey = storey.substring(0, storey.length()-1);
    }
  } else if (key == ENTER) {
    // We submit when the user presses enter
    tokens = (int[]) append (tokens, int(storey));
    // reset
    storey="";
  } else if (key>='0' && key <= '9') {
    storey += key;
  }
}

I try adding the solution u given but it shows “the operator * is undefined for the argument type(s)”

You haven’t answered my questions.

Well, which part are you “adding”?

I changed the type of tokens, did you see?

String storey = “”;
String[] tokens= {}; // split(storey, “,”);
//int[] level ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//int j = 0;
int numbers = 0;

void setup() {
size(1000, 1000);
background (0);

// println(level.length);
}

void draw() {
background (0);

textSize(25);

String caret ="";
if (millis() % 1000<500) {
caret="|";
}
fill(255);
rect (0, 0, 1000, 40);
fill(0);
text( storey + caret, 0, 30);

textSize(15);
fill(255);
text("Please enter your Word. Submit with Enter, You can use Backspace. ",
250, 60);

for (int i = 0; i<tokens.length; i++) {
fill(211, 211, 211);
rect(i * 70+15, 900,
200, 100);
textSize(15);
fill(0);
text(tokens[i],
i * 70+15+3, 900+20);
}
for (int j = 0, j ,tokens.length; j++){
rect(j * 70+15, 900-tokens[j] * 20,
67, tokens[j] * 20);
}
}

void keyTyped() {
if (key == BACKSPACE) {
if (storey.length()>0) {
storey = storey.substring(0, storey.length()-1);
}
} else if (key == ENTER) {
// We split when the user presses enter
tokens = (String[]) append (tokens, storey);
// reset
storey="";
} else {
storey += key;
}
}

I added like that.

Yeah, well tokens is now int[] as I mentioned already

Changed this line as well obviously

1 Like

Ok. Thanks for the help.

1 Like

I don’t know why I cannot add little rectangle to the big rectangle, when I can, it only add one but at the wrong place. Help me solve please.

String storey = “”;
int[] tokens= {}; // split(storey, “,”);

void setup() {
size(1000, 1000);
background (0);
}

void draw() {

textSize(25);

String caret ="";
if (millis() % 1000<500) {
caret="|";
}
fill(255);
rect (0, 0, 1000, 40);
fill(0);
text( storey + caret, 0, 30);

textSize(15);
fill(255);
text(“Please enter your number from 1 to 10. Maximum input is 5.”,
250, 60);

for (int i = 0; i<tokens.length; i++) {
fill(211, 211, 211);
rect(i * 200, 1000-tokens[i]*80, 200, tokens[i]*100);

 int n=6;
 for(int j=0;j<n;j++){
 for(int x=50;x<200;x+=20){
   for(int y=0; y <= tokens.length; y+=50){
     int a = (int) random(1);
     if (a == 1 ){
     fill(0,0,255);
     }
     else {
      fill(0); 
     }
  rect(x+10,100*y-50*y,5,15);
   }
 }
 }

}
}
void keyTyped() {
if (key == BACKSPACE) {
if (storey.length()>0) {
storey = storey.substring(0, storey.length()-1);
}
} else if (key == ENTER) {
// We submit when the user presses enter
tokens = (int[]) append (tokens, int(storey));
// reset
storey="";
} else if (key>=‘0’ && key <= ‘9’) {
storey += key;
}
else if (key==‘n’){ //night time
background(0);
}
else if (key==‘d’){ //day time
background(135,206,235);
}
else if (key==‘m’){ //moon
fill(230,230,250);
ellipse(200, 200, 100, 100);
}
else if (key==‘S’){ //sun
fill(255,255,224);
noStroke();
ellipse(800, 200, 150, 150);
}
else if (key==‘s’){ //star
background(25,25,112);
for(int k=0;k<50;k++){
fill(0,10);
rect(0,0,width,height);
fill(255);
noStroke();
ellipse(random(width),random(height), 3, 3);
}
}
}

I don’t understand. Where are these little rectangles in your code?

Where do they appear now, in the input box on top of the screen?

You have FOUR nested for-loops. That’s a lot. Why so many? What are you trying to achieve?

Below is my version.

Chrisir

Sketch

String storey = "";
int[] tokens= {};

void setup() {
  size(1000, 1000);
  background (0);
}

void draw() {

  textSize(25);

  // Input rectangle 
  fill(255);
  rect (0, 0, 1000, 40);
  fill(0);
  text( storey + caret(), 0, 30);

  // help text 
  textSize(15);
  fill(255);
  text("Please enter your number from 1 to 10. Maximum input is 5.", 
    250, 60);

  for (int i = 0; i<tokens.length; i++) {
    fill(211, 211, 211);
    rect(i * 200, height-tokens[i]*80, 
      200, tokens[i]*80);

    for (int y=0; y < tokens[i]; y++) {
      // int a = (int) random(1);
      if (random(1) < 0.5 ) {
        fill(0, 0, 255);//blue
      } else {
        fill(255, 0, 0);
      }
      rect((i * 200)+(y*10)+9, height-tokens[i]*80+4, 
        5, 15);
    }
  } //for
} //func 

void keyTyped() {
  if (key == BACKSPACE) {
    // delete last letter
    if (storey.length()>0) {
      storey = storey.substring(0, storey.length()-1);
    }
  } else if (key == ENTER) {
    // We submit when the user presses enter
    tokens = (int[]) append (tokens, int(storey));
    // reset
    storey="";
  } else if (key>='0' && key <= '9') {
    storey += key;
  } else if (key=='n') { //night time
    background(0);
  } else if (key=='d') { //day time
    background(135, 206, 235);
  } else if (key=='m') { //moon
    fill(230, 230, 250);
    ellipse(200, 200, 100, 100);
  } else if (key=='S') { //sun
    fill(255, 255, 224);
    noStroke();
    ellipse(800, 200, 150, 150);
  } else if (key=='s') { //star
    background(25, 25, 112);
    for (int k=0; k<50; k++) {
      fill(0, 10);
      rect(0, 0, width, height);
      fill(255);
      noStroke();
      ellipse(random(width), random(height), 3, 3);
    }
  }
}

String caret() {
  // blink 
  String caret ="";

  if (millis() % 1000<500) {
    caret="|";
  }//if

  return caret;
}//func 
//

These are the little rectangle code.
And yes they appear in the input bos on top of the screen.

I am trying to make a building with window. then set night backgroud and all.

Do you like my version?