Need help - very beginner

I have gotten a little school project, should be rather easy but i am completely lost. hope to find some help here.

I am supposed to make something like this:

https://gyazo.com/d9f0a1921c3eea3fc2fe6c691838be37

On the top left i have the number i am currently writing for example “271”, when i hit enter the number is supposed to go down to the 10 next numbers in the left row where the last 10 numbers ive written are. And on the bottom left is the average.

On the top right i have my mouse’s cursor position, when i click left mouse the number will go down to the next 10 numbers in the right row and the last number is the average.

This is the code I have as of right now, however I cant get the enter function to work. please help:

int numbers[] = new int [10];
int currentNumberIndex = 0;
int mouseClicks[] = new int[10];
int currentClickIndex = 0;
String currentNumberString = "";
int aaa = currentNumberString.length();

void setup() {
  size(300,300);
  background(0);
  fill(255,105,180);
  textSize(25);
  textAlign(LEFT, TOP);
}

void draw() {
  clear();
  if(currentNumberString != "") {
    printNumbers(Integer.parseInt(currentNumberString), mouseY, 0);
  }
  else {
    printNumbers(0, mouseY, 0);
  }
  
  for(int i = 0; i < numbers.length; i++) {
    text(Integer.toString(numbers[i]) + "" + Integer.toString(mouseClicks[i]),0,1 + 30 +30);
  }  
}

void mouseReleased() {
  mouseClicks[currentClickIndex] = mouseY;
  currentClickIndex = currentClickIndex == 9 ? 0 : currentClickIndex + 1;
}
  
  

void keyPressed() {
  if(Character.isDigit(key) && aaa <= 5) {
    currentNumberString += key;
  }
  else if (keyCode == 10 && currentNumberString != "") {
    numbers[currentNumberIndex] = Integer.parseInt(currentNumberString);
    currentNumberIndex = currentNumberIndex == 9 ? 0 : currentNumberIndex + 1;
    currentNumberString = "";
  }
}

void printNumbers(int number1, int number2, int row) {
  textAlign(LEFT, TOP);
  text(Integer.toString(number1), 0, row * 30);
  textAlign(RIGHT, TOP);
  text(Integer.toString(number2), 300, row * 30);
}

int calculateAverage(int[] numbers) {
  int sum = 0;
  for(int i = 0; i < numbers.length; i++) {
    sum += numbers[i];
  }
  return sum / numbers.length;
}
1 Like

This == or != won’t work with String

You need to use equals
Use

if( ! currentNumberString.equals(“”) )

1 Like

Still only gives me this https://gyazo.com/bdb3fea9db2551d682e3646087ae8839 wher i am supposed to get https://gyazo.com/d9f0a1921c3eea3fc2fe6c691838be37
im really stuck i dont see a way to make this work

Is this function crucial for that what’s missing?

I noticed you pass 0 as y value?

Try 40 instead.

Shouldn’t you have a for loop?

Also get rid of clear for testing this

1 Like

Although not essentially at the moment I noticed you don’t update aaa

You need to set it anew every time

This appears twice

What’s

again? Return or Enter?

When removing clear the numbers just stack on top of each other
setting it to 40 makes everything go black.

keycode 10 is enter

So removing clear brought us a step forward: we now know that clear deletes our output (that we want to be permanent) AND that it stacks on top (instead of being underneath each other line by line)

Both points need to be adressed separately by you

The first point is that clear deletes where we want to keep it.

Possible solution:

Try set a boolean to true so that you know what and if to display

The second point is that you need better lines underneath each other

So in text() have a y value that you increase after every text() like y+=19;

Why increase after every text?

I thought you meant you had the text stack on top of each other (instead of having it underneath each other line by line)

If this assumption is correct then you want to increase the y value after each text to avoid this

When my assumption was wrong and the columns with the text are just not there you have to make them using a for loop or so

i believe ur assumption was correct. ill give it a shot

1 Like

In draw() your main for-loop was wrong:

The x-value must be 150 (because of textAlign(RIGHT, TOP); in printNumbers() - when I use this textAlign, I usually make another textAlign(LEFT); to go back to default after text(); I haven’t done this here). You could also say textAlign(LEFT); before the for-loop.

and I inserted i*30 at y-value of text() so we got a line-feed

int numbers[] = new int [10];
int currentNumberIndex = 0;
int mouseClicks[] = new int[10];
int currentClickIndex = 0;
String currentNumberString = "";
int aaa = currentNumberString.length();

void setup() {
  size(700, 500);
  background(0);
  fill(255, 105, 180);
  textSize(25);
  textAlign(LEFT, TOP);
}

void draw() {
  clear();
  //translate(55, 50); 
  if (currentNumberString != "") {
    printNumbers(Integer.parseInt(currentNumberString), mouseY, 0);
  } else {
    printNumbers(0, mouseY, 0);
  }

  for (int i = 0; i < numbers.length; i++) {
    text(Integer.toString(numbers[i]) + "" + Integer.toString(mouseClicks[i]), 
      150, 1 + 30 +30 + i * 30);  // x-value 150 and I inserted  i*30  at y-value !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  }
}

void mouseReleased() {
  mouseClicks[currentClickIndex] = mouseY;
  currentClickIndex = currentClickIndex == 9 ? 0 : currentClickIndex + 1;
}

void keyPressed() {
  if (Character.isDigit(key) && aaa <= 5) {
    currentNumberString += key;
  } else if (keyCode == 10 && currentNumberString != "") {
    numbers[currentNumberIndex] = Integer.parseInt(currentNumberString);
    currentNumberIndex = currentNumberIndex == 9 ? 0 : currentNumberIndex + 1;
    currentNumberString = "";
  }
}

void printNumbers(int number1, int number2, int row) {
  textAlign(LEFT, TOP);
  text(Integer.toString(number1), 0, row * 30);
  textAlign(RIGHT, TOP);
  text(Integer.toString(number2), 300, row * 30);
}

int calculateAverage(int[] numbers) {
  int sum = 0;
  for (int i = 0; i < numbers.length; i++) {
    sum += numbers[i];
  }
  return sum / numbers.length;
}
//
2 Likes