# How do i take a input that is space separated?

**URL:** <https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905>\
**Category:** Coding Questions\
**Created:** [January 31, 2022, 2:50am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905 "2022-01-31T02:50:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [January 31, 2022, 2:50am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/1 "2022-01-31T02:50:35Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hi, can anyone advise me on How do I take input that is space-separated? and then putting in a for loop to draw a circle

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [January 31, 2022, 2:36pm UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/2 "2022-01-31T14:36:38Z")

</div>

Hi Zan,

Can you give us more informations: what do you mean by input? a file? a keyboard prompt?mouse position…  
what will be the information? number of circles? positions? other?

cheers

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [January 31, 2022, 2:52pm UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/3 "2022-01-31T14:52:26Z")

</div>

Hello, it would be a keyboard input, for example if the user enter 1 7 8 9 , then 1 7 8 9 would have been the width of the circle

---

<div class="post-metadata">

**Author:** ![fastfourier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fastfourier/32/2764_2.png) [@fastfourier](https://discourse.processing.org/u/fastfourier)\
**Post date:** [January 31, 2022, 3:10pm UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/4 "2022-01-31T15:10:12Z")

</div>

Do you mean you want a String “1 7 8 9” to equal an Integer 1789?

```auto
String s = "1 7 8 9";
int i = parseInt (s.replace(" ", ""));
println (i);

```

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [January 31, 2022, 3:33pm UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/5 "2022-01-31T15:33:27Z")

</div>

Hello,

Not really, so when the user input 1 3 5 9

The system is supposed to extract this one input and put into a loop, so when these 4 number is entered, you will have a circle with the size of 1 3 5 9 respectively shown on the screen

---

<div class="post-metadata">

**Author:** ![fastfourier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fastfourier/32/2764_2.png) [@fastfourier](https://discourse.processing.org/u/fastfourier)\
**Post date:** [January 31, 2022, 3:45pm UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/6 "2022-01-31T15:45:02Z")

</div>

Oh I see.

You need to split the string by `" "` (space), turn it into an array of ints and then loop through that:

```auto
int[] sizes;

void setup() {
  size(400, 200);
  String s = "1 7 8 9 10 55";
  sizes = parseInt(s.split(" "));
}

void draw() {
  int x=20;
  for (int i=0; i<sizes.length; i++) {
    ellipse(x, height/2, sizes[i], sizes[i]);
    x+=30;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [January 31, 2022, 4:14pm UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/7 "2022-01-31T16:14:52Z")

</div>

int sizes;

void setup() {  
size(400, 200);  
}

void draw() {  
int x=20;  
for (int i=0; i\<sizes.length; i++) {  
ellipse(x, height/2, sizes[i], sizes[i]);  
x+=30;  
}  
}

void keyTyped() {  
if (key == BACKSPACE) {  
if (buffer.length()\>0) {  
buffer = buffer.substring(0, buffer.length()-1);  
}  
}  
else if(key == ENTER) {

sizes = parseInt(buffer.split(" "));  
buffer = “”;  
}  
else{  
buffer = buffer + key;  
}  
}

> Blockquote

If I want to work in a text box , does it work like this?

---

<div class="post-metadata">

**Author:** ![fastfourier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fastfourier/32/2764_2.png) [@fastfourier](https://discourse.processing.org/u/fastfourier)\
**Post date:** [January 31, 2022, 5:10pm UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/8 "2022-01-31T17:10:29Z")

</div>

Looks like it should work.

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [February 5, 2022, 8:14am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/9 "2022-02-05T08:14:50Z")

</div>

When i try to run it gave me an error “NullPointerException” what should i 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:** [February 5, 2022, 8:27am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/10 "2022-02-05T08:27:20Z")

</div>

> [@Zan](#):
>
> int sizes;

must be int sizes = new int[20];

---

<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:** [February 5, 2022, 8:37am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/11 "2022-02-05T08:37:24Z")

</div>

> [@Zan](#):
>
> for (int i=0; i\<sizes.length;

And try if(sizes.length \> 0) before it

AND say String buffer=“”; before setup ()

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [February 5, 2022, 8:56am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/12 "2022-02-05T08:56:21Z")

</div>

> [@Chrisir](#):
>
> String buffer=“”

I tried this but it seems like there no text box there anymore

---

<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:** [February 5, 2022, 8:57am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/13 "2022-02-05T08:57:38Z")

</div>

Did you say it before setup () ?

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [February 5, 2022, 9:06am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/14 "2022-02-05T09:06:33Z")

</div>

Yup

````auto
int[] sizes= new int[20];
String buffer="";

void setup() {
size(400, 200);
}

void draw() {
int x=20;
if(sizes.length > 0){
for (int i=0; i<sizes.length; i++) {
ellipse(x, height/2, sizes[i], sizes[i]);
x+=30;
}
}
}

void keyTyped() {
if (key == BACKSPACE) {
if (buffer.length()>0) {
buffer = buffer.substring(0, buffer.length()-1);
}
}
else if(key == ENTER) {

sizes = parseInt(buffer.split(" "));
buffer = "";
}
else{
buffer = buffer + key;
}
}```
````

---

<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:** [February 5, 2022, 9:10am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/15 "2022-02-05T09:10:01Z")

</div>

this works here

```auto

int[] sizes = new int[20];
String buffer=""; 
boolean bl; 

void setup() {
  size(400, 200);
  background(111);
}

void draw() {
  background(111); 
  int x=20;
  // if (sizes.length > 0)
  for (int i=0; i<sizes.length; i++) {
    ellipse(x, height/2, sizes[i], sizes[i]);
    x+=30;
  }
  text(buffer+blink(), 19, 19);
}

void keyTyped() {
  if (key == BACKSPACE) {
    if (buffer.length()>0) {
      buffer = buffer.substring(0, buffer.length()-1);
    }
  } else if (key == ENTER) {

    sizes = parseInt(buffer.split(" "));
    buffer = "";
  } else {
    buffer = buffer + key;
  }
}

String blink() {
  if (frameCount%19==0) 
    bl=!bl; 
  if (bl)
    return "|";
  else return "";
}

```

---

<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:** [February 5, 2022, 9:10am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/16 "2022-02-05T09:10:36Z")

</div>

> [@Zan](#):
>
> ```auto
> sizes = parseInt(buffer.split(" "));
> buffer = "";
> 
> ```

don’t say `buffer = "";` here …

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [February 5, 2022, 9:18am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/17 "2022-02-05T09:18:51Z")

</div>

Thank you! i will try it out

---

<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:** [February 5, 2022, 9:24am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/18 "2022-02-05T09:24:44Z")

</div>

You can change your circles to rectangles

Just use rect command

And some additional changes for placement I guess

---

<div class="post-metadata">

**Author:** ![Zan](https://avatars.discourse-cdn.com/v4/letter/z/48db29/32.png) [@Zan](https://discourse.processing.org/u/Zan)\
**Post date:** [February 5, 2022, 10:04am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/21 "2022-02-05T10:04:03Z")

</div>

How do you make sure the input u key in will fit the screen entirely, for example, if you key 3 numbers you can only see 3 of them and not have a empty space

---

<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:** [February 5, 2022, 10:23am UTC](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905/22 "2022-02-05T10:23:13Z")

</div>

That’s math

Let’s say input 3 4 1 - sum is 8

divide width by 8 that’s your scale (3 times scale, 4 times scale etc. … )

Change x accordingly

[Next page](https://discourse.processing.org/t/how-do-i-take-a-input-that-is-space-separated/34905.md?page=2)
