How do i take a input that is space separated?

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

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

1 Like

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

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

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

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

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

Oh I see.

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

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;
  }
}

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?

Looks like it should work.

When i try to run it gave me an error “NullPointerException” what should i do

must be int[] sizes = new int[20];

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

AND say String buffer=""; before setup ()

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

Did you say it before setup () ?

Yup

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;
}
}```

this works here



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 "";
}

don’t say buffer = ""; here …

Thank you! i will try it out

1 Like

You can change your circles to rectangles

Just use rect command

And some additional changes for placement I guess

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

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