Java (processing.or...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] Java (processing.org) help. Again.

17 Posts
6 Users
0 Reactions
40 Views
Posts: 12
Free Member
Topic starter
 

Hi all, trying to write the code which will make a tree. It's a Uni question. The code looks like it does as it's filled previous questions / criteria.

I need all of values in the Tree class to be passed in as parameters.

My code is so close to working (I think). But, compiling it, I get a "Bad character: n" error. It doesn't tell me which line it is either.

I can't find anything about it through google.

I know that saying, there's an error somewhere, but don't know where isn't much help but I'm stuck as a stuck thing.

I need this code to run so that I can move on to the next question, and the next etc.

Any help would be massively appreciated. My code is.

Tree tree;

void setup() {
int SZ = 512; // screen size

int d = 2;
int x = SZ/2;
int y = SZ;

float m_branchAngle = (25.7/180.0)*PI;
float m_initOrientation = -HALF_PI;
String m_state = "F";
float m_scaleFactor = 1;
String m_F_rule = "F[+F]F[-F]F";
String m_H_rule = "";
String m_f_rule = "";
int m_numIterations = 5;

size(SZ,SZ);
background(255);
noLoop();

tree = new Tree(d, x, y, m_branchAngle, m_initOrientation, m_state, m_scaleFactor, m_F_rule, m_H_rule, m_f_rule, m_numIterations);
}

void draw() {
tree.draw();
}

_______________________________________________________________________

and

_______________________________________________________________________

class Tree {

// member variables
int m_lineLength; // turtle line length
int m_x; // initial x position
int m_y; // initial y position
float m_branchAngle; // turtle rotation at branch
float m_initOrientation; // initial orientation
String m_state; // initial state
float m_scaleFactor; // branch scale factor
String m_F_rule; // F-rule substitution
String m_H_rule; // H-rule substitution
String m_f_rule; // f-rule substitution
int m_numIterations; // number of times to substitute

// constructor
// (d = line length, x & y = start position of drawing)
Tree(int d, int x, int y, float m_branchAngle, float m_initOrientation, String m_stateTemp, float m_scaleFactor, String m_F_rule, String m_H_rule, String m_f_rule, int m_numIterations) {
m_lineLength = d;
m_x = x;
m_y = y;
m_branchAngle = (25.7/180.0)*PI;
m_initOrientation = -HALF_PI;
m_scaleFactor = 1;
m_state = m_stateTemp;
m_F_rule = "F[+F]F[-F]F";
m_H_rule = "";
m_f_rule = "";
m_numIterations = 5;

// Perform L rounds of substitutions on the initial state
for (int k=0; k < m_numIterations; k++) {
m_state = substitute(m_state);
}
}

void draw() {
pushMatrix();
pushStyle();

stroke(0);
translate(m_x, m_y); // initial position
rotate(m_initOrientation); // initial rotation

// now walk along the state string, executing the
// corresponding turtle command for each character
for (int i=0; i < m_state.length(); i++) {
turtle(m_state.charAt(i));
}

popStyle();
popMatrix();
}

// Turtle command definitions for each character in our alphabet
void turtle(char c) {
switch(c) {
case 'F': // drop through to next case
case 'H':
line(0, 0, m_lineLength, 0);
translate(m_lineLength, 0);
break;
case 'f':
translate(m_lineLength, 0);
break;
case 's':
scale(m_scaleFactor);
break;
case '-':
rotate(m_branchAngle);
break;
case '+':
rotate(-m_branchAngle);
break;
case '[':
pushMatrix();
break;
case ']':
popMatrix();
break;
default:
println("Bad character: " + c);
exit();
}
}

// apply substitution rules to string s and return the resulting string
String substitute(String s) {
String newState = new String();
for (int j=0; j < s.length(); j++) {
switch (s.charAt(j)) {
case 'F':
newState += m_F_rule;
break;
case 'H':
newState += m_F_rule;
break;
case 'f':
newState += m_f_rule;
break;
default:
newState += s.charAt(j);
}
}
return newState;
}

}


 
Posted : 10/03/2015 1:39 am
Posts: 17
Free Member
 

Not sure if it's just a copy/paste but you have mismatched "}" last one isn't paired with anything.


 
Posted : 10/03/2015 1:53 am
Posts: 4954
Free Member
 

Have you tried commenting out section so you just have class and function bare bone definitions then adding bit back in until you get you bad character error?

Crude but if you have a useless error and have not been compiling as you go it can help you track down your problem.


 
Posted : 10/03/2015 1:54 am
Posts: 0
Free Member
 

Bear in mind I'm tired... and I haven't poked at Processing.org in years, but if x is half your draw height and y is your full with, could there be an issue with you attempting to draw outside the box? Iirc it draws from top left to bottom right... could some of your maths be putting stuff too far out on the Y-Axis? If m_y = 0 instead of y... does that fix it?


 
Posted : 10/03/2015 2:13 am
Posts: 12
Free Member
Topic starter
 

mikewsmith - it must be a copy and paste mistake- or it's hiding as the code lost its formatting on STW. Removing the last "}" gets a "} was expected" error.

The Brick - I haven't.

I'll try but, I'm working beyond my knowledge! Way beyond. It's partly luck that's got me to this point (and a disproportionate amount of time for the work produced too). I'll struggle to know exactly what to comment out.

Is it something to do with the substitution at the bottom?

For the life of me, I can't see what.

edit: thanks swamptin but no, that throws up so many errors I didn't even read them all 🙂


 
Posted : 10/03/2015 2:15 am
Posts: 0
Free Member
 

Edit - Just saw your reply... I'll leave my thinking cap on. If I spot something I'll try again. Otherwise, best of luck.


 
Posted : 10/03/2015 2:24 am
Posts: 17
Free Member
 

no worries, was just the fast error checking in Notepad++
http://notepad-plus-plus.org/download/v6.7.4.html
Great for checking that sort of thing and editing in general


 
Posted : 10/03/2015 2:38 am
Posts: 0
Free Member
 

I was just thinking, when you're in here;

// apply substitution rules to string s and return the resulting string
String substitute(String s) {
String newState = new String();
for (int j=0; j < s.length(); j++) {
switch (s.charAt(j)) {
case 'F':

Is 's' NULL/""? charAt(j) = NULL. Could that be part of it? Or have I missed where 's' is defined?


 
Posted : 10/03/2015 2:48 am
Posts: 12
Free Member
Topic starter
 

I don't think S is defined anywhere.

That part of the code was supplied to us. Whilst I understand the gist of what it's doing, I can't see how to solve the error.

Someone's told me

Now you just need to hunt down the line that's changing m_state from "F" to "null". Hint: It's somewhere in substitute().

But, I can't hunt down the line! 🙁


 
Posted : 10/03/2015 2:58 am
Posts: 17
Free Member
 

// Perform L rounds of substitutions on the initial state
for (int k=0; k < m_numIterations; k++) {
m_state = substitute(m_state);
}
}

Search for m_state as the substitute(Has Stuff Inside the Brackets)

CtrF FTW


 
Posted : 10/03/2015 3:00 am
 poly
Posts: 8699
Free Member
 

I'd hazard a guess that it is not a compilation error at all - and is caused by the line where you tell it to output "Bad character: " + c in some circumstances!

The fact you didn't realise that's what you had written makes me think you stole someone else's code!


 
Posted : 10/03/2015 3:02 am
Posts: 12
Free Member
Topic starter
 

Thanks mikewsmith. Which m_state do you mean? The constructor? The member variables?

Poly. I said, somewhere up there ^^^

That part of the code was supplied to us

About 60% of the code is mine, by now. We were given code and lots of questions / targets; Modify the code to do x. Now make it do y.

So I haven't stolen it, but also didn't claim it was mine.

I did mis-use the idea of a compiler error though.


 
Posted : 10/03/2015 3:15 am
Posts: 17
Free Member
 

That would be up to you, I don't speak Java just other big checking plays a big part of my life. Depending on which editor you are using something like N++ makes life easy for checking, searching and finding things. Does all the highlighting stuff that helps.


 
Posted : 10/03/2015 3:33 am
 poly
Posts: 8699
Free Member
 

A crude but often effective way of seeing inside your executing code - (and it does look like m_state not containing what you think might be the problem) is to litter it with print statements. So I would be adding println("Tell me I have just entered {some] function")
println("m_state now set to "+m_state)
and
println("Returning {variable name] = "+variable)

throughout the code.

Your IDE may well have tools to save you the trouble.

However this line: case 'F': // drop through to next case
scream at me that something is odd. Do you mean not to break there?


 
Posted : 10/03/2015 3:45 am
Posts: 12
Free Member
Topic starter
 

Thanks Poly.

I haven't written any of the near the bottom.

At the previous stage, my code looked like this


class Tree {

// member variables
int m_lineLength; // turtle line length
int m_x; // initial x position
int m_y; // initial y position
float m_branchAngle; // turtle rotation at branch
float m_initOrientation; // initial orientation
String m_state; // initial state
float m_scaleFactor; // branch scale factor
String m_F_rule; // F-rule substitution
String m_H_rule; // H-rule substitution
String m_f_rule; // f-rule substitution
int m_numIterations; // number of times to substitute

// constructor
// (d = line length, x & y = start position of drawing)
Tree(int d, int x, int y) {
m_lineLength = d;
m_x = x;
m_y = y;
m_branchAngle = (25.7/180.0)*PI;
m_initOrientation = -HALF_PI;
m_scaleFactor = 1;
m_state = "F";
m_F_rule = "F[+F]F[-F]F";
m_H_rule = "";
m_f_rule = "";
m_numIterations = 5;

// Perform L rounds of substitutions on the initial state
for (int k=0; k < m_numIterations; k++) {
m_state = substitute(m_state);
}
}

void draw() {
pushMatrix();
pushStyle();

stroke(0);
translate(m_x, m_y); // initial position
rotate(m_initOrientation); // initial rotation

// now walk along the state string, executing the
// corresponding turtle command for each character
for (int i=0; i < m_state.length(); i++) {
turtle(m_state.charAt(i));
}

popStyle();
popMatrix();
}

// Turtle command definitions for each character in our alphabet
void turtle(char c) {
switch(c) {
case 'F': // drop through to next case
case 'H':
line(0, 0, m_lineLength, 0);
translate(m_lineLength, 0);
break;
case 'f':
translate(m_lineLength, 0);
break;
case 's':
scale(m_scaleFactor);
break;
case '-':
rotate(m_branchAngle);
break;
case '+':
rotate(-m_branchAngle);
break;
case '[':
pushMatrix();
break;
case ']':
popMatrix();
break;
default:
println("Bad character: " + c);
exit();
}
}

// apply substitution rules to string s and return the resulting string
String substitute(String s) {
String newState = new String();
for (int j=0; j < s.length(); j++) {
switch (s.charAt(j)) {
case 'F':
newState += m_F_rule;
break;
case 'H':
newState += m_F_rule;
break;
case 'f':
newState += m_f_rule;
break;
default:
newState += s.charAt(j);
}
}
return newState;
}

}

and


Tree tree;

void setup() {
int SZ = 512; // screen size

int d = 2;
int x = SZ/2;
int y = SZ;

size(SZ,SZ);
background(255);
noLoop();

tree = new Tree(d, x, y);
}

void draw() {
tree.draw();
}

and it worked.

The issue has been since I "extended the constructor so values for all of the tree member variables can be passed in as parameters."

I extended the constructor and the tree values. I got a NullException error so changed m_state to m_state_Temp and now get the bad character issue.

The code I've posted here (my code from the previous 'stage') works fine. Does that mean that the "F" //drop to next case could still be the problem?

Thanks,

Mike


 
Posted : 10/03/2015 4:02 am
Posts: 12
Free Member
Topic starter
 

I wa being an idiot.

I shouldn't have been assigning the variables a value in the class.

The class shouldn't have said


m_y = y;
m_branchAngle = (25.7/180.0)*PI;
m_initOrientation = -HALF_PI;
m_scaleFactor = 1;

should have been more like


m_branchAngle = Angle
m_initOrientation = inOr

and then edit other code accordingly, giving them a value in Tree tree, such as


float Angle = -THIRD_PI

Thanks for all replies.

Mike


 
Posted : 10/03/2015 6:44 am
Posts: 2616
Full Member
 

In the wonderfull way that internet help cxan sound wrong/awkward I hope this does not.

but to stick in my pennysworth & this way you will learn yourself.

Break it down in to simple methods & write simple junit tests.


 
Posted : 10/03/2015 8:15 am
Posts: 12
Free Member
Topic starter
 

Thanks MadBill.

j unit testing certainly looks useful but, at the moment, more of a long term solution. I can see (with my 2 minute google) how that would help though.

Coding is a tough teach-yourself subject. It's one of those things where someone with knowledge can explain something that could take you weeks of reading and going down dead ends.

Thankfully, the rest of the degree has very little coding involved.

Some XHTML next year (yup, nothing written for the course for a few years 🙂 ) and then done.


 
Posted : 10/03/2015 9:52 am

6 DAYS LEFT
We are currently at 95% of our target!