A Problem with a piece of code for making a table

edited February 2017 in Library Questions

if i run the following program, the arrays x and y are sometimes empty and i dont know why.

P.S. the Input boxes are arranged from top down first and then from left to right.

PP.S. how can I change the formatting of this code to look like code

import org.gicentre.utils.stat.*;
import controlP5.*;
ControlP5 cp5;
Textfield[] a = new Textfield[80];
Button b;
XYChart lineChart;
float first_x = 50, first_y = 20;
boolean show_chart = false;
int lastnumber = 80;
float[] x, y;


void setup() {
  size(1200, 600);
  background(51);
  initialize();
  b = cp5.addButton("zeichnen").setPosition(first_y, 500+first_x).setSize(260, 20);
  lineChart = new XYChart(this);
}

void draw() {
  line(20, 247+first_x, 279, 247+first_x);
  if (show_chart) {
    lineChart.draw(first_x+260, first_y, width-30, height-30);
  }
}


//Button
void zeichnen() {
  //setting the arrays to the right length (!)
  for (int i = a.length-1; !(i<0); i--) {
    if (!"0".equals(a[i].getText())) {
      lastnumber = i;
    }
  }
  println(lastnumber);
  x = new float[lastnumber];
  y = new float[lastnumber];
  //copying the arrays
  for (int i = 0; i<y.length; i++) {
    x[i] = 10*i;
  }
  for ( int i = 0; i < x.length; i++ ) {
    y[i] = float(a[i].getText());
  }
  //(?)
  lineChart.setData(x, y);
  //formating
  lineChart.showXAxis(true); 
  lineChart.showYAxis(true); 
  lineChart.setMinY(0); 
  lineChart.setYFormat("00");
  lineChart.setXFormat("000"); 
  lineChart.setPointColour(color(180, 50, 50, 100));
  lineChart.setPointSize(5);
  lineChart.setLineWidth(2);
  //(works)
  show_chart = true;
}



//make all Textboxes (works)
void initialize() {
  PFont font = createFont("arial", 16);
  cp5 = new ControlP5(this);
  for (int i = 0; i<a.length; i++) {
    a[i] = cp5.addTextfield(str(i)).setFont(font).setColor(color(255)).setColorBackground(0).setColorForeground(0).setText("0").setLabelVisible(false).setSize(50, 20);
    if (i<20) {
      a[i].setPosition(first_y, first_x+25*i);
    } else if (i<40) {
      a[i].setPosition(first_y+70, first_x+25*(i-20));
    } else if (i<60) {
      a[i].setPosition(first_y+140, first_x+25*(i-40));
    } else if (i<80) {
      a[i].setPosition(first_y+210, first_x+25*(i-60));
    } else {
      println("too much objects for in a[]");
    }
  }
}

Answers

  • Go back

    Edit your post with gear wheel upper right corner

    Now leave a empty line before and after the code

    Now select entire code with mouse

    Hit ctrl-o or the C in the small command bar

  • Answer ✓

    the arrays x and y are sometimes empty and i dont know why

    You never assign a value to any of the textFields. You only assign them a value of "zero" when they are initialize, line 69. To test this idea, add between line 69 and 70: a[i].setText(""+i); and tun your code.

    Kf

  • edited February 2017

    the Problem was fixed by breaking out of the for loop when the last Number is found and adding one to the arrays x and y : thank you kfrajer for bringing me on the path for the solution

    import org.gicentre.utils.stat.*;
    import controlP5.*;
    ControlP5 cp5;
    Textfield[] a = new Textfield[80];
    Button b;
    XYChart lineChart;
    float first_x = 50, first_y = 20;
    boolean show_chart = false;
    int lastnumber = 80;
    float[] x, y;
    
    
    void setup() {
      size(1200, 600);
      background(51);
      initialize();
      b = cp5.addButton("zeichnen").setPosition(first_y, 500+first_x).setSize(260, 20);
      lineChart = new XYChart(this);
    }
    
    void draw() {
      background(51);
      line(20, 247+first_x, 279, 247+first_x);
      if (show_chart) {
        lineChart.draw(first_x+260, first_y, width-340, height-30);
      }
    }
    
    
    //Button
    void zeichnen() {
      //setting the arrays to the right length (!)
      for (int i = a.length-1; !(i<0); i--) {
        println(a[i].getText());
        if (!"0".equals(a[i].getText())) {
          lastnumber = i+1;
          break;
        }
      }
      println(lastnumber);
      x = new float[lastnumber];
      y = new float[lastnumber];
      //copying the arrays
      for (int i = 0; i < x.length; i++) {
        x[i] = 10*i;
      }
      for ( int i = 0; i < y.length; i++ ) {
        y[i] = float(a[i].getText());
      }
      //(?)
      lineChart.setData(x, y);
      //formating
      lineChart.showXAxis(true); 
      lineChart.showYAxis(true);
      lineChart.setMinX(0); 
      lineChart.setMinY(0); 
      lineChart.setYFormat("00");
      lineChart.setXFormat("000"); 
      lineChart.setPointColour(color(180, 50, 50, 100));
      lineChart.setPointSize(5);
      lineChart.setLineWidth(2);
      //(works)
      show_chart = true;
    }
    
    
    
    //make all Textboxes (works)
    void initialize() {
      PFont font = createFont("arial", 16);
      cp5 = new ControlP5(this);
      for (int i = 0; i<a.length; i++) {
        a[i] = cp5.addTextfield(str(i)).setFont(font).setColor(color(255)).setColorBackground(0).setColorForeground(0).setText("0").setLabelVisible(false).setSize(50, 20);
        if (i<20) {
          a[i].setPosition(first_y, first_x+25*i);
        } else if (i<40) {
          a[i].setPosition(first_y+70, first_x+25*(i-20));
        } else if (i<60) {
          a[i].setPosition(first_y+140, first_x+25*(i-40));
        } else if (i<80) {
          a[i].setPosition(first_y+210, first_x+25*(i-60));
        } else {
          println("too much objects for in a[]");
        }
      }
    }
    
Sign In or Register to comment.