 # Can't handle the loop

Hi guys! This is a pretty basic problem: I don’t fully understand how to use loops.
I want to get comfortable with using arrays and other elementary stuff so I came up with the idea of creating some kind of licence plate generator.

``````int[] numbers = new int;
char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W' };
String[] plates = new String;

String a = "B-" +
chars[int(random(chars.length)) ] +
numbers[int (random(numbers.length) )] +
chars[int(random(chars.length)) ] +
chars[int(random(chars.length))] +
numbers[int (random(numbers.length) )] ;

void setup() {
size(400, 400, P2D);
frameRate(1);
for (int i=1; i<numbers.length; i++) {
numbers[i]= (int)i;
}
}

void draw() {
background(155);
textSize(58);
textAlign(LEFT);
text(a, 30, height/2);

for (int i=0; i<plates.length; i++) {
plates[i]= a;
}

if (plates!=null) {
println(plates);
println(plates);
println(plates);
println(plates);
println(plates);
println(plates);
println(plates);
println(plates);
println(plates);
println(plates);
}
}
``````

It’s supposed to take entities of the numbers and char array to create a plate and save it in a dedicated array (plates). I (think I) understand the problem, but can’t come up with a solution.
Right now it generates a code and saves it to the array, but for every position.

Can anybody please give me a hint, like what buzzword should I search for or explain it to me right away?

Hope the formatting is OK and I’ll appreciate any help =)

I came up with a solution:

``````int[] numbers = new int;
char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W' };
String[] plates = new String;

String a;
int i;

void setup() {
size(400, 400, P2D);
frameRate(999);
for (int i=1; i<numbers.length; i++) {
numbers[i]= (int)i;
}
}

void draw() {
a= "B-" +
chars[int(random(chars.length) )  ] +
numbers[int (random(numbers.length) )  ] +
chars[int(random(chars.length) )  ] +
chars[int(random(chars.length) )  ] +
numbers[int (random(numbers.length) )  ] ;

background(155);
textSize(58);
textAlign(LEFT);
text(a, 30, height/2);

plates[i] = this.a;

i=i+1;

if (i>=100) {
noLoop();
printArray(plates);
}
}
``````
2 Likes

Hi losferatos,

You code is not properly formatted. To format it, edit your previous post, select all your code and hit the `</>` icon in the toolbar of the text editor. Don’t forget to select your code and hit `ctrl + t` in the precessing IDE in order to indent everything correctly.

Hi see that you find a solution to your problem and that you post it, thanks for that To complement on your solution, instead of using `noLoop()` you can simply put everything in the `setup()` function and not even write the `draw()` one 1 Like

Thanks, jb4x!

But regarding the noLoop thingy: isn’t it necessary to have the a-String inside the draw, otherwise it wouldn’t update? At least following my understanding of the code.

But anyway, I’m one step closer to archiving what I thought about.
I managed to create a certain amount of this codes and I’m also able to save them to a text file.

Now I want to improve this by adding a graphical user input box that sets the amount of codes to create.
I stumbled upon ControlP5 but isn’t there a built-in way? It doesn’t need to look nice, it just needs to work^^.

But the new problem is: I set-up a text box, but I need integer input only and Numbox doesn’t suit my needs.

Any ideas? I’d prefer to not use ControlP5 because it looks quite complex and I’m about to get used to structure and commands in processing which already is quite overwhelming haha

``````import controlP5.*;
ControlP5 cp5;

String a;
int i;
int laenge=500;
boolean send;

int[] numbers = new int;
char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W' };
String[] plates = new String[laenge];

void setup() {

cp5 = new ControlP5(this);

size(400, 400, P2D);
frameRate(999);
for (int i=1; i<numbers.length; i++) {
numbers[i]= (int)i;
}
}

void draw() {
background(155);

if (send==true) {
a = "B-" +
chars[int(random(chars.length) )  ] +
numbers[int (random(numbers.length) )  ] +
chars[int(random(chars.length) )  ] +
chars[int(random(chars.length) )  ] +
numbers[int (random(numbers.length) )  ] ;

textSize(58);
textAlign(LEFT);
text(a, 30, height/2);

plates[i] = this.a;

i=i+1;

if (i>=laenge) {
noLoop();
saveStrings("plates.txt", plates);
printArray(plates);
}
}
}

void Send() {
send=true;
//laenge=cp5.get(Textfield.class,"inputfield").getText();
}
``````

Whenever I need to use text boxes of sort, I just stick with G4P! It’s great, and also, props to @quark as it is his library, and if you have any questions about it you have someone to ask Take a look at the references here:
http://www.lagers.org.uk/g4p/ref/index.html

But yes, G4P is very friendly for inputs and stuff, and makes it a great experience to do so.

I also did some looking about the number only input thing, and found this:

So hopefully you’re able to piece together what it is you are trying to do. Hopefully this helps!

EnhancedLoop7

try:

``````import javax.swing.JOptionPane;

int N = 0;

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!"); }
return I;
}

//_________________________________________________________________ SETUP
void setup() {
}
``````

you can combine it with a Button Menu like button ask number

Actually, I didn’t see that you had some animation process, my bad.

But if you don’t care about the visual effect, you can generate your plate completely in the setup function using a for loop (that emulates what you are doing in the draw function). That’s what I meant.

Something like this:

``````int[] numbers = new int;
char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W' };
String[] plates = new String;

String a;
int i;

void setup() {
size(400, 400, P2D);
frameRate(999);
for (int i=1; i<numbers.length; i++) {
numbers[i]= (int)i;
}

for (int i=0; i<100; i++) {
a= "B-" +
chars[int(random(chars.length) )  ] +
numbers[int (random(numbers.length) )  ] +
chars[int(random(chars.length) )  ] +
chars[int(random(chars.length) )  ] +
numbers[int (random(numbers.length) )  ] ;

plates[i] = this.a;
}

printArray(plates);
}
``````

For the GUI part, I would give you the same advise as @EnhancedLoop7: use G4P, it is quite intuitive to start with The forum discussion linked by @EnhancedLoop7 is no longer valid and the programmer must not attempt to change the value displayed in a textfield or textarea control while it has focus it will crash the sketch). This means that it is not possible to constrain the textfield input to an integer by ignoring non-numeric characters.

Instead we can test the input as it changes and give a visual warning if invalid.

The sketch below has a single GTextField control and the text colour changes to red when the input value is not an integer and black when OK. The event handler code can easily be modified to perform other types of validation e.g. accepting float values only…

``````/**
Sketch to demonstrate how to check that the input of a GTextField
is an integer.

When the textfield has focus the input value is tested everytime
it changes (e.g. when a key is pressed.

If the input is a valid integer the text remains black but when
invalid the text is red.

NOTE: you must not programatically modify the textfiled input
value while the control has focus.

created by Peter Lager 2018

*/
import g4p_controls.*;

GTextField txf; // the text field we want to enter an interger
int n;          // the last valid integer entered.

void setup() {
size(300, 200);
txf = new GTextField(this, 20, 30, 200, 20);
}

void draw() {
background(200, 200, 255);
}

void handleTextEvents(GEditableTextControl textcontrol, GEvent event) {
if (textcontrol == txf) {
if (event == GEvent.CHANGED) {
try {
n = Integer.parseInt(txf.getText());
txf.setLocalColor(2, color(0));
}
catch(java.lang.NumberFormatException e) {
txf.setLocalColor(2, color(255, 0, 0));
}
}
}
}
``````
4 Likes

@quark, very nice,
but just to show that it works can use

``````void draw() {
background(200, 200, 255);
fill(0); text(n,240,45);
}

``````
1 Like