# Receiving int data as IntList

**URL:** <https://discourse.processing.org/t/receiving-int-data-as-intlist/15826>\
**Category:** Coding Questions\
**Created:** [November 26, 2019, 10:05am UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826 "2019-11-26T10:05:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 26, 2019, 10:05am UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/1 "2019-11-26T10:05:27Z")

</div>

Hi! I am making a code for drawing a graph. I am receiving data(int) in an IntList form, and something is not going right.  
It is supposed to get data like

1. First, enter the title.
2. Enter the number of rows.
3. Then enter all of your data. (only one-digit numbers, no space, only the amount of rows that you entered in 2)

but in 3, it keeps printing strange numbers that keep changing. It is driving me crazy.☹  
If you have some free time, please help me solve this problem. Or if you have an alternative for getting numbers, please advise me.  
I need your help badly… The source code is on the bottom.

```auto

void setup(){
  background(0);
  size(1000, 1000);
  textSize(30);
  text("*GraphSaver Beta Instructions*\n\nPress the white button on the right top to end program.\n\n1.First enter the title.\n2.Then enter the number of rows.\n3.Then enter all of your data.\n4.A graph will be made.press enter to save and delete to erase it. \n\nWhen you finish reading click the screen.", 20, 30);
  arr = new IntList();
}

int num=0;
String title = "";
IntList arr;

void draw(){
  square(950, 0, 50);
  
  textSize(30);
  if(keynum==0)text(title, 100, 100);
  if(keynum==1 && num != 0){
    text(num-48, 100, 100);
  }
  /*
  if(keynum==2){
    for(int i = 0; i < arr.size(); i++){
      arr.sub(i, 48);
      text(arr.get(i), 100, 100);
    }
  }*/
  int multi = 80;
  int plus = 150;
  
  }
}

void mousePressed(){//End process
  background(0);
  if(mouseX > 950 && mouseY < 50){
    fill(200, 11, 22);
    square(950, 0, 50);
    delay(5000);
    print("END");
  }
}

void mouseMoved(){
  if(mouseX > 950 && mouseY < 50){
    fill(200, 11, 22);
    square(950, 0, 50);
  }
  else {
    fill(255);
    square(950, 0, 50);
  }
}

int imsi=0;//temp
int keynum=0;//how nany times key has been pressed-0:title, 1: number of rows, 2 : data

void keyPressed(){//input
  background(0);
  if(key == ENTER){keynum++;return;}
  if(keynum == 0)title += key;
  if(keynum == 1){
    num = key;
  }
 if (keynum == 2) {
    arr.append(key - 48);
    imsi++;
    println(arr);
  }
}

```

plus, I will put this code together with the code below to make a whole program. Just in case the answering person wants it, I’ll post it together.

```auto
void setup(){
  background(0);
  size(4000, 4000);
  textSize(30);
  arr = new IntList();
  arr.append(8);
  arr.append(2);
  arr.append(3);
  arr.append(4);
  arr.append(5);
  arr.append(9);
  arr.append(6);
  arr.append(7);
  arr.append(1);
  
}

int num=9;
String title = "Untitled #1";
IntList arr;

void draw(){
/* for(int i = 0; i < 3; i++){
    print(" ", arr.get(i));
  }
 */ 
 int multi = 80;
 int plus = 150;
 textSize(50);
 text(title, 250, 70);
 textSize(30);
  stroke(255);
  line(100, 100, 100, arr.max()* multi + 50 + 100);
  line(100, arr.max()* multi + 50 + 100, 100 + num * 120 + 150, arr.max()* multi + 50 + 100);
  for(int i = 0; i < num; i++){
    rect(plus, arr.max()* multi + 50 + 100 - arr.get(i) * 80, 100, arr.get(i) * 80);
    fill(1);
    text(arr.get(i), 40 + plus, arr.max()* multi + 100);
    fill(255);
    textSize(20);
    text(i + 1, 40 + plus, arr.max()* multi + 190);
    textSize(30);
    plus += 150;
  }
  
 
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2019, 10:09am UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/2 "2019-11-26T10:09:14Z")

</div>

please use a topic title what tells us what your question is about,  
think about thousand other people search this forum  
for similar questions they have,  
and find only titles like “HELP…”

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 26, 2019, 10:10am UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/3 "2019-11-26T10:10:17Z")

</div>

## Yes:) I’'m sorry

---

<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:** [November 26, 2019, 12:38pm UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/4 "2019-11-26T12:38:44Z")

</div>

In the first code:

In draw and keyPressed work with if…else if… throughout! Otherwise when we increase keynum the next condition would also apply.

---

<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:** [November 26, 2019, 12:59pm UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/5 "2019-11-26T12:59:59Z")

</div>

I just realized that this is not the issue here probably

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2019, 1:31pm UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/6 "2019-11-26T13:31:50Z")

</div>

> [@24stevek](#):
>
> Or if you have an alternative for getting numbers, please advise me.

when i started to make [this](http://kll.engineering-news.org/kllfusion01/downloads/RPI_processing34_418.jpg)  
i used the

```auto
import javax.swing.JOptionPane;

// for button menu ask setpoint input
// _________________________________________________________________ askI call: A = askI("A",A);
int askI(String ask, int I) {
  String r = JOptionPane.showInputDialog(null, "new Setpoint for "+ask+" (now "+I+" )", "Input (INT)", JOptionPane.QUESTION_MESSAGE);
  if (r == null ) { 
    print(" NULL "); 
    r = str(I);
  } // handle CANCEL
  try { 
    I = Integer.parseInt(r);
  } 
  catch(NumberFormatException e) { 
    println("you did not enter a int number!");
  }
  println("new "+ask, I);
  return I;
}

// _________________________________________________________________ askF call: A = askF("A",A);
float askF(String ask, float F) {
  String r = JOptionPane.showInputDialog(null, "new Setpoint for "+ask+" (now "+F+" )", "Input (FLOAT)", JOptionPane.QUESTION_MESSAGE);
  if (r == null ) { 
    print(" NULL "); 
    r = str(F);
  } // handle CANCEL
  try { 
    F = Float.parseFloat(r);
  } 
  catch(NumberFormatException e) { 
    println("you did not enter a int or float number!");
  }
  println("new "+ask, F);
  return F;
}

int a = 0;
float b = PI;
void setup() {
  a = askI("input a ", a);
  b = askF("input b ", b);
  println("a "+a+" b "+b);
}
void draw() {
}

```

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [November 26, 2019, 8:37pm UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/7 "2019-11-26T20:37:04Z")

</div>

Hi there 24stevek,

If I understand you correctly, the number of rows you enter in the second step should function as a ‘limit’ for the amount of data you can insert in the third step. Is that correct? If so, the problem is that this limit isn’t being set, since you can keep adding numbers to the IntList.  
The numbers that you see are the length of the InList, followed by the numbers that you’ve entered. I noticed that when you click on letters it adds different numbers (for instance, my letter ‘b’ adds the value of 50). Is that intended?

I have to say that your current sketch looks a bit messy. My advice is to redo your sketch from scratch and follow my suggested structure below. In addition I would only add _necessary_ functionalities at first. Set up your sketch so the flow of the sketch works, then start adding one functionality at a time. That way your code stays clean and understandable. Finally add details, such as a description and an exit button.

```auto
int num = 0, imsi = 0;
IntList arr;
// other global variables

void setup() {
  size(1000, 1000);
  // other setup stuff
}

void draw() {
  // local variables
  // draw stuff
}

// keyboard 

// mouse functions

```

A last tip I suggest you look at [switch](https://processing.org/reference/switch.html). Using this structure you could easily go back and forth in your program. For instance, left click jumps back a page while right mouse click goes to the next page. Perhaps implementing such a structure might help you keep your code a bit clearer.

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 27, 2019, 1:22pm UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/8 "2019-11-27T13:22:30Z")

</div>

Yes! I’ll follow your advice. First, thank you for even reading my code. Even I myself didn’t want to look at my code because it was VERY messy. Maybe the only answer is to rewrite it from scratch. With only key functions.  
Second, I guess that the strange numbers added when you press B is the ASCII code? b has the ASCII number of 98, and I made it subtract 48 and add it to the IntList. That is because the ASCII code for numbers from 0~9 is “the original number”+ 48. For example, the ASCII code of 1 is 49.  
Last, how can I set up the limit of the IntList? Thank you.

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [November 27, 2019, 1:51pm UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/9 "2019-11-27T13:51:52Z")

</div>

You’re welcome! Not sure if it’s ASCII codes, but personally I think using letters to enter numbers complicates things. Might not be a bad plan to only use numbers to make your sketch a bit less confusing for others 😉

In the second step you enter the limit, which I assume will be saved to a variable. When you’re in the third step, you should check whether this limit is reached. If not, you can add another number.  
A way to do that is by using an `if` statement, which compares the ‘limit’ with the length of your [IntList](https://processing.org/reference/IntList.html).

---

<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:** [November 27, 2019, 2:03pm UTC](https://discourse.processing.org/t/receiving-int-data-as-intlist/15826/10 "2019-11-27T14:03:41Z")

</div>

> [@24stevek](#):
>
> how can I set up the limit of the IntList

Yes, store the limit in a variable

Have a counter that counts the inputs

Compare both with if (counter\>=…)

when true, go to next state
