Creating primitive data types from contents of String

J

Jesper Sahner

Hi!

Let's say that s is a String containing a name. How do I then
(dynamically) create an integer with that specific name like:

String s="abc";
int abc;

String s="def";
int def;

....

E.g. the String-names could be some names read from a file.

Regards,
Jesper
 
J

Joona I Palaste

Jesper Sahner said:
Let's say that s is a String containing a name. How do I then
(dynamically) create an integer with that specific name like:
String s="abc";
int abc;
String s="def";
int def;

E.g. the String-names could be some names read from a file.

You can't. Variable names, at least local ones (method scope) are an
entirely compile-time concept and can't be manipulated at run-time.
What are you really trying to accomplish with this?
 
O

Oscar kind

Jesper Sahner said:
Let's say that s is a String containing a name. How do I then
(dynamically) create an integer with that specific name like:

String s="abc";
int abc;

You don't. After all: how would you reference it?

If you need to create labels for numbers/objects/... dynamically, use a
Map. Also note that you cannot use primitive types like int, char,
boolean, etc. Use Integer, Crahacer, Boolean, etc. instead.
 
A

Alberto

I'm not sure why would you like to do that. However, if you are trying
to dynamically create an object depending on input entered, you may
want to read about "Reflection". Keep in mind this will only work on
data objects, not on primitives.

You won't be able to create a primitive, they are not objects. You
could use wrapper classes (Objects) for those primitives (or you could
write your own). If you still insit in storing and defining a variable
from input, a good approach could be to use a data structure (create
your own object) to do the abstraction for you.
 
J

Jesper Sahner

Hi again!

Consider the following problem:
You have some information stored in a data-file, and in addition you
have a header-file with a description of the record-layout
(variable-name, position, length, format, label etc.).

The task then is to read from the data-file using the
header-information. If e.g. the header contains a description of a
variable 'var1' of type 'double' and another variable 'var2' of type
'int' then the Java-code should declare these variables on basis of
the header-information like:
double var1;
int var2;

Then you could make some calculations involving var1 and var2, write
the result to a new file etc. - very similar to a database-lookup.

How would you do this?

Regards,
Jesper
 
S

Sudsy

Jesper Sahner wrote:
The task then is to read from the data-file using the
header-information. If e.g. the header contains a description of a
variable 'var1' of type 'double' and another variable 'var2' of type
'int' then the Java-code should declare these variables on basis of
the header-information like:
double var1;
int var2;

Then you could make some calculations involving var1 and var2, write
the result to a new file etc. - very similar to a database-lookup.

Again, your motive escapes me (and others, apparently).
A variable is merely a convenient handle to either a primitive or an
Object. The value or object being referenced has no need to know the
name(s) (if any) which refer to it.
BTW, I just LOVED the post which mentioned the scenario of foo( 3 )!
Guess what? There's no variable name associated with the value!
Please think carefully about what you're trying to achieve. It might
turn out to be impossible, and for good reason.
Perhaps if you describe what you're trying to accomplish? ...
 
C

Casey Hawthorne

Does the following page describe what you are trying to do?

http://mindprod.com/jgloss/eval.html

eval
In many languages you can take a dynamically created String such as
"6*(4+6^2)-cos(20)" and ask to have it evaluated, as if it were a
miniature computer program. The function to do this often has a name
such as eval. Java has no such function. What can you do? Here are
four different approaches:
....
 
H

hiwa

(e-mail address removed) (Jesper Sahner) wrote in message
I would write a simple text processing program which generate a
relevant part -- variable declaration part -- of the target Java
source code.

As mentioned earlier, names in the source program are only relevant to
compiler. In order to feed them to the compiler, we can not use
nothing but ordinary source code. We have to make source text from
your header etc.
 
J

Jesper Sahner

Hi!

As I have described the task is somewhat similar to a database-lookup.

It is true that a variable is just a reference to an object or a
primitive type. However it is important that these variables have
names corresponding to the header-information. If the header states,
that the file to be read contains two variables 'var1' and 'var2' of
type 'double' and 'int' then it is important that they are given the
right names 'var1' and 'var2' when read from the file, because
typically the code following the read depends on the right names -
like a database-lookup.

Regards,
Jesper
 
J

Joona I Palaste

Jesper Sahner said:
As I have described the task is somewhat similar to a database-lookup.
It is true that a variable is just a reference to an object or a
primitive type. However it is important that these variables have
names corresponding to the header-information. If the header states,
that the file to be read contains two variables 'var1' and 'var2' of
type 'double' and 'int' then it is important that they are given the
right names 'var1' and 'var2' when read from the file, because
typically the code following the read depends on the right names -
like a database-lookup.

I still can't fathom why you think you have to use actual variable names
for this. If the field names are so important, just use a big Map where
the keys are the names "var1" and "var2" etc. and the values are Doubles
or Integers or whatever objects you use to encapsulate what you read
from the file.
Trying to dynamically read or write variable names in the program source
code at run-time is pretty much always a sign of a design problem or a
misunderstanding of how compiled languages work.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
 
O

Oscar kind

Jesper Sahner said:
As I have described the task is somewhat similar to a database-lookup.

It is true that a variable is just a reference to an object or a
primitive type. However it is important that these variables have
names corresponding to the header-information.

Why do you need to use a variable? Why can't you attach that name to the
value using an associative collection (i.e. a Map)?

If the header states,
that the file to be read contains two variables 'var1' and 'var2' of
type 'double' and 'int' then it is important that they are given the
right names 'var1' and 'var2' when read from the file, because
typically the code following the read depends on the right names -
like a database-lookup.

Code cannot use a runtime-defined variable name, as the compiler cannot
compile that (it cannot reference what doesn't exist). It can however, use
a fixed variable to hold the name of another variable, and look up its
value in a Map (and store that value in another fixed variable).

Then again, if what you're doing is like a database lookup, why not use a
database? If it is just for configuration, use a Properties object
(similar to a Map<String,String>, but with load/store capabilities). If it
is more complex, you often want to store business data, and then a
database is most likely the best choice.

So regardless of what you do: make sure your design is right. If you
cannot give a good reason for each design choice, nor explain why it is
better than the alternatives, you've made the wrong choice.
 
C

Chris Smith

Jesper said:
As I have described the task is somewhat similar to a database-lookup.

It is true that a variable is just a reference to an object or a
primitive type. However it is important that these variables have
names corresponding to the header-information. If the header states,
that the file to be read contains two variables 'var1' and 'var2' of
type 'double' and 'int' then it is important that they are given the
right names 'var1' and 'var2' when read from the file, because
typically the code following the read depends on the right names -
like a database-lookup.

Unfortunately, you seem to be unable to transcend the specific
implementation that you've decided that you want. Let me be entirely
clear: what you want to do is not possible, nor even sensible, in Java.
If you write code that uses 'var1' and 'var2' without declaring them
yourself, then your code won't compile.

Now, for the umpteenth time, what are you trying to do?

Depending on what you're trying to do, perhaps the following would work:

double var1 = Double.NaN, var2 = Double.NaN;

As you're reading the file,

if (name.equals("var1")) var1 = Double.parseDouble(value);
else if (name.equals("var2")) var2 = Double.parseDouble(value);
...

And then you can write your code:

return var1 * Math.sin(var2);

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

John C. Bollinger

Joona said:
Trying to dynamically read or write variable names in the program source
code at run-time is pretty much always a sign of a design problem or a
misunderstanding of how compiled languages work.

And it is rarely, if ever, good design even in languages where it might
be made to work. (E.g. Perl) Such languages, by the way, must
necessarily handle the situation internally in a manner similar to the
Map-based approach suggested elsewhere in this thread for handling it in
Java.


John Bollinger
(e-mail address removed)
 
A

Alberto

Although you are not describing too much about your project/assignment,
it sounds to me that you are trying to build some sort of a small
interpreter Somewhat like Perl and scalars (which instead of a
compiler, it uses an interpreter). If this is your real goal, you need
to remember that interpreters are really using a lot of abstractions.
Most of them use a lookup table (although there are many other ways to
simulate this) keeping record of the variables during the run-time
(e.g. Hashmap, Hashtables). For all you care, their values (or even
types if you want to get more fancy) can be stored as strings. These
values can later be interpreted to their correct meaning according to
your design.

Once again, it would help you a lot if you start thinking of
generating the abstraction that those two variables var1 and var2 are
working together, rather than looking to have the compiler do the
interpretation for you. Remember that in this realm of thinking about
"abstractions", there are many approaches you could take.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top