Jump to content

Need a little bit of help with java table

Muffin123

So i have to code a brainfuck interpreter for school. Ive made most of it work by making it go into a table and then the thing going trough it correcting the values and moving along as it should to come to an answer, the problem i have is when i come to [ and ], so how [ works is if the value of the cell where [ comes in is != 0, the program will move to the next order, but when the value of that is 0 it will need to skip a few places ahead untill it finds ] and then continue with the next step from the ].

 

Now the problem is i dont know how to write that, ive made a case statement:

case '[':    if (t[c] != 0) {    break;    } else {     //in this case it needs to find ]   }  case ']':    if (t[c] == 0) {    break;    } else {    //in this case it needs to find [    }

So what i need to put in is how to skip a few places in a table untill it finds ] and then +1 the index © of that cell to go to the next one where it should continue

Link to comment
Share on other sites

Link to post
Share on other sites

If you just need to find the next ], then can't you use a loop.

For Example:

while(i != ']') {increment to next char}

Not sure how you are reading the file, and if you need to know the value in between the [ and ], I would somehow save the index values of the brackets or even save the value in a string in the while loop.

Link to comment
Share on other sites

Link to post
Share on other sites

If you just need to find the next ], then can't you use a loop.

For Example:

while(i != ']') {increment to next char}

Not sure how you are reading the file, and if you need to know the value in between the [ and ], I would somehow save the index values of the brackets or even save the value in a string in the while loop.

I dont need the value inbetween i just need to completely skip all the indexes untill ] and continue from the index of ] +1. Does this just jump to the next char? cuz the values inbetween are also chars, all is composed by + - < > , . and []

Link to comment
Share on other sites

Link to post
Share on other sites

You could use a boolean. Set it in the switch case and at the beginning of your loop add

if (booleanVariable && character != ']') continue;
Link to comment
Share on other sites

Link to post
Share on other sites

 

You could use a boolean. Set it in the switch case and at the beginning of your loop add

if (booleanVariable && character != ']') continue;

How do you mean that, sorry im kind of a beginner so i really dont understand what you meant here

Link to comment
Share on other sites

Link to post
Share on other sites

How do you mean that, sorry im kind of a beginner so i really dont understand what you meant here

 

What don't you understand?

 

If you're not familiar with the branching statement continue, check this out.

 

If you're not familiar with the logical operator &&, check this out.

 

If something else is confusing you, let me know.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×