ImplementIon of eval() parser and 2d array in Java?

Just do: class Foo { private String table_of_classifiers = null; void bar() { table_of_classifiers = new String { {"x1","x","x","x","x"}, {"x2","x","x","x","x"}, {"x3","x","x","x","x"}, {"x4","x","x","x","x"}, {"x5","x","x","x","x"}, {"x6","x","x","x","x"}, }; } } Java doesn't have eval (because it's a compiled language), but it does have reflection. It's almost certainly not the best approach to whatever it is that you want to do, though.

Thank you Oli! I will keep that answerer! Can you please be more specific regarding your eval() solution?

– eualin Dec 19 '11 at 15:20.

Regarding the creation of a 2D array, I initialize a table like this: private String table_of_classifiers = null; Not really. This is the declaration and initialization of a variable that can point to a "2d array", "table" or more exact an "array of arrays" of Strings. Unless you work with that fact that the variable can/will be null, initializing it to null is usually a bad idea, because you need to do extra work to check for null.

Examples: String a; // ... String be = a00; This won't compile, unless a wasn't initialized in the mean time. This is a good thing, because you can avoid a potential bug. String a = null; // ... String be = a00; This will however will compile, and if you forgot to actually assign the variable a real array, the program will "crash" with a "null pointer exception" or you need to add additional code/work to check for null.

I fill its contents like this: String table_of_classifiers = { {"x1","x","x","x","x"}, {"x2","x","x","x","x"}, {"x3","x","x","x","x"}, {"x4","x","x","x","x"}, {"x5","x","x","x","x"}, {"x6","x","x","x","x"}, }; You are not "filling" anything here. For something to be filled it must exist first, but you haven't created anything yet. Here you are declaring a second variable of the same name, which is only possible if you are in a different scope that the first one, and in that case you are "hiding" ("shadowing") the original variable if it originally was accessible from this new scope.

But as you can guess the second table overwrites (locally) the first one, that is of course not what I want to do. What I do wrong? Which "first" table?

There was no first table until now, only a first variable. The others have shown you what you need to do to assign the "table" to the original variable, by not using the "declaration" String at the beginning of the line. Otherwise it's impossible to say what you are "doing wrong" because you haven't really explained what you are attempting to do.

Note that the dimension of the table is not known from the beginning. It's not? How/why are you using a array literal then?

Literal arrays are for creating arrays of a fixed size with a fixed "prefilling". What exactly do mean with "the beginning"? Isn't the size known when you are programming (during compile time) or when the program starts (at run time)?

If you get the size of the array during run time you can create a normal array with new: int a = ...; int be = ...; // Get the sizes from somewhere, e. G, user input String table_of_classifiers = new Stringab; // Now you have an "empty" table If size "changes" during run time, then - depending on what you are actually attempting to do - then an array is the wrong tool and you should be using a List implementation such as ArrayList instead. Regarding "eval", as the others say, Java is a compiled language making "eval" basically impossible.

The is "reflection" or the use of Class types to achieve what you are hinting at, but you really need to explain much more extensively what you are trying to achieve, then it may be possible to help you here. However reflection and CLass types are a complicated matter, and considering you are obviously struggling with the most basic Java concepts, you have a long way to go to until you will be able to do what you want to do.

Thank you RoToRa. However, there was no need of giving so much details! The answer was much more easy that you first thought!

– eualin Dec 19 '11 at 15:08.

Regarding your first problem: to assign to table_of_classifiers without redeclaring it, write: table_of_classifiers = new String { {"x1","x","x","x","x"}, {"x2","x","x","x","x"}, {"x3","x","x","x","x"}, {"x4","x","x","x","x"}, {"x5","x","x","x","x"}, {"x6","x","x","x","x"}, }; Regarding eval . . .

The problem is that the run-time doesn't have the names of scoped local variables, and although it can get the names of instance variables, it has to do that within the context of an object. It's possible to address these sorts of issues, but it's non-trivial, and will involve major compromises. I think you have to thoroughly understand how scoping works and how reflection works before you start figuring out what features eval will support, because otherwise you'll just be disappointed at all the requirements you give it that turn out to be impossible.

Thank you ruakh! However, I was looking for a two-step implementation. Could you please help me on how to create the parser?

It is very important for me because I will then be able to make more a compact and maintainable code! – eualin Dec 19 '11 at 15:22 @eualin: I promise you that creating your own eval, writing your own parser from scratch, will not make for more compact and maintainable code. The rest of your code may be more compact, but the parser itself will be huge, complicated, and buggy.

– ruakh Dec 19 '11 at 15:34 Thanks @ruakh. So, what you would recommend me? Any specific example/approach?

I don't think we can't find a solution to that problem... I am not saying it is trivial, I am just saying that it shouldn't be so difficult at all, at least, given the aforementioned requirements... – eualin Dec 19 '11 at 17:07 @eualin: I'm a bit confused. You've already accepted Oli Charlesworth's answer. Are you not satisfied with it?

– ruakh Dec 19 '11 at 17:44 Had you read that comment more carefully, you could understand that I am still trying to figure out how to implement a hand-made eval() function for Java. Any ideas? – eualin Dec 19 '11 at 19:42.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions