I am designing code to turn characters into an array of characters into and int and vice versa. the character array should look like this "AA1" or "GE5" and the int is a value anywhere from 0 to 7010. The problem with this code is that the last line in the class where the float is converted to a char array in the last lines of code it returns a blank instead of the number for the last value in the char array. Please help.
So i am designing code to convert a string such as ("AA3") to and integer and vice versa. The integer is the result of a nested loop set up that i have been trying to get to work.
for(int i = 0;i < letters.length;i++) {// first loop checks what character the first space is and starts filling the integer.
if (chid[0] == letters[i]) {
outputf = 270 * (int)letters[i];
}
for (int h = 0;h < letters.length;h++) { // second loop checks the second character and applys it to the int.
if(chid[1] == letters[h]) {
outputs = 10 * (int)letters[h];
}
for (int a = 0; i < numbers.length - 1;a++) { //third loop checks the number at the end for its value.
if(chid[2] == numbers[a]) {
outputt = 1 * numbers[a];
}
}
}
}
return outputf + outputs + outputt; //returns the sum of all three spaces.
}
The problem ive been encountering is that on line "if(chid[2] == numbers[a]) {" ive been getting an array out of bounds exception which means i did something wrong with the arrays.
If someone could just help me to first get rid of this error and secondly get this program to work for its intended purpose.
This is my code and the bug i am having is that whenever i click the mouse peasy cam seems to reset to 0,0,0. I have scoured the code 20 some times and i cannot find the problem.
//TODO::: freeze camera
//TOFDO: move to selected particle
//TODO::REdo all the tabs and update to current standards.
import processing.opengl.*;
import controlP5.*;
import peasy.*;
PGraphics buffer; // buffer
Cube[] cubes; // cubes
PeasyCam cam;
ControlP5 controlP5;
PGraphics3D g3;
PMatrix3D currCameraMatrix;
int Xi;
int Yi;
int Zi;
int idSave;
void setup() {
size(1400, 700, OPENGL);
frameRate(25);
frame.setTitle("Variable Geoetry Neo-Fluid Prototype 1.2.1");
g3 = (PGraphics3D)g;
buffer = createGraphics(width, height, P3D);
cam = new PeasyCam(this, 10);
cam.setResetOnDoubleClick(false);
cam.setMinimumDistance(50);
controlP5 = new ControlP5(this);
controlP5.addButton("button",10,100,60,80,20).setId(1);
controlP5.addButton("buttonValue",4,100,90,80,20).setId(2);
controlP5.addButton("Create Geometry",4,10,500,100,30);
controlP5.addButton("Hotfix1",40,100,60,80,20);
controlP5.addButton("Hotfix2",40,100,80,80,20);
controlP5.setAutoDraw(false);
controlP5.addSlider("sliderValue",0,255,128,100,200,10,100);
controlP5.addSlider("slider",100,200,128,100,160,100,10);
controlP5.addTextfield("Serial Output",100,160,200,20);
controlP5.addTextfield("Number of Particles",10,300,200,20);
controlP5.addToggle("Static or Dynamic?",false,10,350,80,20).setMode(ControlP5.SWITCH);
controlP5.addTextlabel("label1","Company",300,630);
controlP5.addTextlabel("label2","Unit",150,630);
controlP5.addTextlabel("label3","Identifier",10,630);
// put cubes randomly in the scene
cubes = new Cube[75];
for (int i = 0; i < cubes.length; i++) {
cubes[i] = new Cube(
i, // identifiant
Xi = -15 + (int)random(300), // position x
Yi = -15 + (int)random(300), // position y
Zi = -15 + (int)random(300), // position z
5 + (int)random(150) // taille
);
}
}
void draw() {
hint(ENABLE_DEPTH_TEST);
background(255);
lights();
for (int i = 0; i < cubes.length; i++) {
cubes[i].display(this.g);
}
cubes[4].moveUp();
hint(DISABLE_DEPTH_TEST);
gui();
}
void mouseClicked() {
// draw the scene in the buffer
buffer.beginDraw();
buffer.background(getColor(-1)); // since background is not an object, its id is -1
buffer.noStroke();
buffer.setMatrix(g3.camera);
for (int i = 0; i < cubes.length; i++) {
cubes[i].drawBuffer(buffer);
}
buffer.endDraw();
// get the pixel color under the mouse
color pick = buffer.get(mouseX, mouseY);
// get object id
int id = getId(pick);
// if id > 0 (background id = -1)
if (id >= 0) {
// change the cube color
cubes[id].changeColor();
}
println(id);
idSave = id;
}
// id 0 gives color -2, etc.
color getColor(int id) {
return -(id + 2);
}
// color -2 gives 0, etc.
int getId(color c) {
return -(c + 2);
}
class Cube {
// variables
int id; // id
int x, y, z, w; // position (x, y, z) and width (w)
color c; // color (in scene, not buffer)
// constructor
public Cube(int id, int x, int y, int z, int w) {
this.id = id;
this.x = x;
this.y = y;
this.z = z;
this.w = w;
c = color(random(255),200, random(255));
}
// make the color change
public void changeColor() {
int r = (int)red(c);
int g = (int)green(c);
int b = (int)blue(c);
c = color(r, 255 - g, b);
}
// display the cube on screen
public void display(PGraphics ecran) {
ecran.fill(c);
drawCube(ecran);
}
// draw the cube in the buffer
public void drawBuffer(PGraphics buffer) {
color idColor = getColor(id);
buffer.fill(idColor);
drawCube(buffer);
}
private void drawCube(PGraphics g) {
g.pushMatrix();
g.translate(x, y, z);
g.box(5);
g.popMatrix();
}
private void moveLeft() {
g.translate(x++,y,z);}
private void moveRight() {
g.translate(x--,y,z);}
private void moveUp() {
g.translate(x,y++,z);}
private void moveDown() {
g.translate(x,y--,z);}
private void moveStop() {
g.translate(0,0,0);}
private void ToParticle(){
println(idSave);
}
OK so i was just trying to write a class that writes a bunch of different types of variables into one string,After a few gours research and construction i was getting near completion. Towards the end of the projsect i kep getting the "Syntax error maybe missing right parenthesis. Here is my code:
import processing.serial.*;
Serial myPort;
void Setup() {
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
class Comm {
string id;
boolean e1;
boolean e2;
boolean e3;
boolean e4;
boolean e5;
boolean e6;
int XPn;
double Xdist;
int YPn;
double Ydist;
int ZPn;
double Zdist;
};
Comm(string tempid, boolean tempe1, boolean tempe2, boolean tempe3, boolean tempe4, boolean tempe5, boolean tempe6, int tempXPn, double tempXdist, int tempYPn, double tempYdist, int tempZPn, double tempZdist) {