# I have a problem to ask

**URL:** <https://discourse.processing.org/t/i-have-a-problem-to-ask/18500>\
**Category:** Coding Questions\
**Created:** [March 8, 2020, 1:15pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500 "2020-03-08T13:15:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![marika20](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marika20](https://discourse.processing.org/u/marika20)\
**Post date:** [March 8, 2020, 1:15pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/1 "2020-03-08T13:15:50Z")

</div>

Goodmorning everyone,  
I have a problem with a code I’m working on but I don’t understand what’s wrong, someone who can help me out?

```auto
int randomNum;
//boolean contenitore []; 
boolean[] contenitoreVario = new boolean [5];
boolean Positivo = true; 
boolean Negativo = false; 

void setup() {
  size(400, 400);
  background(127);
  frameRate(1);
}
}
void draw() {
  randomNum = random(100);
}

for (int i = 0; i < 1; i++) { 
  if (randomNum <50); 
  contenitore[] = positivo;
 else if (randomNum >50);
contenitore[] = negativo; 
}

println(contenitore); 

```

thank!

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [March 8, 2020, 1:54pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/2 "2020-03-08T13:54:55Z")

</div>

Add your code inside draw:

```java
void draw() {
  for (int i = 0; i < 50; i++) {
    contenitore = random(100);
  }
  printArray(contenitore);
}

```

I suggest you explore the [examples](https://processing.org/examples/) available in the main website.

Kf

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [March 8, 2020, 1:58pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/3 "2020-03-08T13:58:33Z")

</div>

In addition, you can format the code in the forum by selecting your code and hitting the formatting code button which looks like this: `</>`. Formatting code is a good practice to ensure your code is not modified by the Forum markdown processor when it is published plus it makes your code easier to read.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c07c089a4402fb8c686d04b86233bcc557bffa61.png)

Kf

---

<div class="post-metadata">

**Author:** ![marika20](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marika20](https://discourse.processing.org/u/marika20)\
**Post date:** [March 8, 2020, 2:38pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/4 "2020-03-08T14:38:53Z")

</div>

thanks, but unfortunately it gives me error in any case ☹

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 8, 2020, 3:08pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/5 "2020-03-08T15:08:23Z")

</div>

> [@marika20](#):
>
> boolean contenitore ;  
> boolean contenitore = new boolean [5];  
> boolean Positivo = true;  
> boolean Negativo = false;
> 
> void setup() {  
> size(400, 400);  
> background(127);  
> frameRate(1);  
> }
> 
> void draw() {  
> }
> 
> for (int i = 0; i \< 50; i++) {  
> contenitore = random(100);  
> }
> 
> printArray(contenitore);

You have declared the array `contenitore` twice in the first two lines. So delete the first line.

The ‘=’ sign is the assignment operator, it means evaluate what is on the right-hand-side and store it in the variable on the left-hand-side. The data types for both sides must be the same. So

> [@marika20](#):
>
> contenitore = random(100);

won’t work because `contenitore` is an _ **array of booleans** _ and the RHS returns a _ **float** _

replacing it with `contenitore[i] = random(100);` won’t work because `contenitore[i]` is a _ **boolean** _ and `random(100)` returns a float.

As @kfrager points out you can’t have code outside of a function.

I suggest you look at the examples and simple tutorials for Processing so you understand how code is supposed to be organised. In particular look at the tutorial on `setup()` and `draw()`.

This is your first time here so I will give you a hint on how to get better help.

Statements like

> [@marika20](#):
>
> I have a problem with a code I’m working on but I don’t understand what’s wrong, someone who can help me out?

and

> [@marika20](#):
>
> but unfortunately it gives me error in any case

are useless because they don’t say what is wrong or what the error is. You need to give much more information on what you expected to happen, what actually happened and details of any error messages.

BTW welcome to the forum 😄

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 8, 2020, 3:29pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/6 "2020-03-08T15:29:55Z")

</div>

Hello,

Welcome to the Processing forum.

Please read the Processing FAQ and Guidelines:  
**1**. [https://discourse.processing.org/faq](https://discourse.processing.org/faq)  
**2**. [Guidelines - Tips on Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147)  
**3**. [Guidelines - How to Answer Questions](https://discourse.processing.org/t/guidelines-how-to-answer-questions/2145)

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

Lots of resources on the Processing website to start you on your journey:  
[https://processing.org/](https://processing.org/)

`:)`

---

<div class="post-metadata">

**Author:** ![marika20](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marika20](https://discourse.processing.org/u/marika20)\
**Post date:** [March 8, 2020, 4:06pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/7 "2020-03-08T16:06:16Z")

</div>

🤔  
ok thanks very kind, thanks for the “welcome” possibly today is the first day!  
Anyway I didn’t understand much ☹  
What should I do to get the correct code?

Thank you!

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 8, 2020, 4:46pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/8 "2020-03-08T16:46:36Z")

</div>

> [@marika20](#):
>
> What should I do to get the correct code?

What are you trying to achieve?

---

<div class="post-metadata">

**Author:** ![marika20](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marika20](https://discourse.processing.org/u/marika20)\
**Post date:** [March 8, 2020, 5:55pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/9 "2020-03-08T17:55:58Z")

</div>

sorry if I ask you but I need :), I should initialize a boolean type array containing five elements, then assign to each value of the array if it is false or true by establishing it with a ‘for loop’ and at each cycle generate a random from zero to one hundred and if the number was less than 50 then it would be TRUE, otherwise FALSE.  
This is what I’m trying to figure out how to do it.  
you are very kind, thank you 🥰

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 8, 2020, 6:00pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/10 "2020-03-08T18:00:49Z")

</div>

Is this school work?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 8, 2020, 6:15pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/11 "2020-03-08T18:15:38Z")

</div>

I am going to assume that this is school work about arrays so I am not going to give you a fully worked answer but help you do it for yourself.

Since there is no graphics we don’t need a draw() method.

The for loop needs to be in a function in this case `setup()` so the basic code is

```auto
// Global variables

// setup function which is executed once when the sketch starts
void setup(){
  // All your code will go here
}

```

In terms of global variables you only need the boolean array and an int for the random number which you have already created so lets add that and some pseudocode for the your code

```auto
// Global variables
int randomNum;
boolean[] contenitoreVario = new boolean [5];

// setup function which is executed once when the sketch starts
void setup(){
  // pseudocode V V V V V V V V V 
  // for each element in our array
  // get a random number in the range >=0 and < 100 and store it in our variable
  // if the number is less than 50
  // store TRUE in the current array element
  // else
  // store FALSE in the current array element
  // end if 
  // end for
  // print array
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 8, 2020, 6:22pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/12 "2020-03-08T18:22:44Z")

</div>

Inside your for loop you have

> [@marika20](#):
>
> contenitore = negativo;

This will not work for two reasons

1. `negativo` is undefined because the global variable is `Negativo` and in Java variable names are case sensitive so these are two different variables.
2. `contenitore[]` represent the whole array you need `contenitore[i]` to represent the current array element

I think it is time for you to try again. Please post your sketch below instead of editing the original post.

---

<div class="post-metadata">

**Author:** ![marika20](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marika20](https://discourse.processing.org/u/marika20)\
**Post date:** [March 8, 2020, 8:06pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/13 "2020-03-08T20:06:02Z")

</div>

no, I don’t go to school anymore, she is a friend of mine who needs, I had done something when I went to school but some time has passed 😰

---

<div class="post-metadata">

**Author:** ![marika20](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marika20](https://discourse.processing.org/u/marika20)\
**Post date:** [March 8, 2020, 8:08pm UTC](https://discourse.processing.org/t/i-have-a-problem-to-ask/18500/14 "2020-03-08T20:08:06Z")

</div>

thank you, very kind 😊
