Receiving int data as IntList

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.:frowning:
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.


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.

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

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…”

1 Like

Yes:) I’'m sorry

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.

1 Like

I just realized that this is not the issue here probably

1 Like

when i started to make this
i used the

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() {
}


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.

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. 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.

3 Likes

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.

1 Like

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 :wink:

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.

2 Likes

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

3 Likes