How can I solve this Puzzle???Java String

H

heinz

I have read a String,which called "Input a,b,c;" from a txt file
and now I wonna write it into java Format,chang it into really Input a,b,c;
just like a sentence in java.
How can I do it from the string into the file?

Thanks
 
R

Roedy Green

I have read a String,which called "Input a,b,c;" from a txt file
and now I wonna write it into java Format,chang it into really Input a,b,c;
just like a sentence in java.
How can I do it from the string into the file?

I don't understand. Literally what you said requires no conversion.
Just a readLine to String and a write back.

See http://mindprod.com/fileio.html
and
http://mindprod.com/converter.html

to generate the code for the usual file i/o and conversions usually
STring <=> something else.
 
J

Joona I Palaste

Roedy Green said:
On 18 Jun 2004 17:40:44 -0700, (e-mail address removed) (heinz) wrote or
quoted :
I don't understand. Literally what you said requires no conversion.
Just a readLine to String and a write back.
to generate the code for the usual file i/o and conversions usually
STring <=> something else.

AFAIK what he wants is to read a String from a file and interpret it as
Java code at run-time. In other words, when he has read the String
"Input a,b,c;" he wants it to declare three variables a, b and c, each
of type Input, in the current method.
This is impossible in Java, or pretty much any other compiled
imperative language. He might have some hope writing new Java source
code on the fly and invoking the Java compiler API, but modifying the
currently executing program is impossible, so forget about that.
 
R

Roedy Green

AFAIK what he wants is to read a String from a file and interpret it as
Java code at run-time. In other words, when he has read the String
"Input a,b,c;" he wants it to declare three variables a, b and c, each
of type Input, in the current method.
This is impossible in Java, or pretty much any other compiled
imperative language

If that is what you want, try http://mindprod.com/jgloss/parser.html
and http://mindprod.com/jgloss/eval.html

I thought of one other interpretation. If you want to read
comma-separated value lists, see http://mindprod.com/jgloss/csv.html
 
C

Chris Smith

heinz said:
I have read a String,which called "Input a,b,c;" from a txt file
and now I wonna write it into java Format,chang it into really Input a,b,c;
just like a sentence in java.
How can I do it from the string into the file?

You're question isn't very clear, so I'm going to make a few guesses.

Do you mean that the String initially has quotes, and you want to remove
them? If so, String.substring can do the trick.

If you want to plug in arbitrary code into a Java source file during the
build process, there are a number of utilities to do that. You might
consider m4, which is a very powerful macro processor that can
accomplish tasks like this.

Or do you want to execute that code? In that case, you've got quite a
task in front of you. Java is a compiled language, so you can't
interpret source code at runtime. You *can* call the Java compiler,
assuming that the full J2SDK is installed rather than just the JRE, but
you will need to wrap that code fragment in it's own class. You
certainly can't declare any local variables that would be accessible in
the current method (and if you could, how would you access them since
you don't know their names when you're writing the app?).

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

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

Joona I Palaste

that's what the eval entry is about.

AFAIK that still doesn't count as modifying the code of the currently
executing program. What your Java glossary entry suggests is two
different approaches:
1) Dynamically write, compile and load a new class.
2) Compose an expression in a language-within-Java and evaluate it.
Neither of these approaches can declare new variables in the currently
executing method, which seems to be what the OP wants to do.

Here, look at a more concrete example.

public class Foobar {
public static void main(String[] args) {
String line = getALineFromSomewhere();
/* Assume the above String is "int a;" */
MagicalClass.doMagicStuff(line);
a=1;
System.out.println("The value of a is " + a);
}
}

If such a magical class existed, this code would print:
The value of a is 1

As it stands, the code won't even compile, because a is never
defined anywhere. At run-time, it would be defined, but how the heck
is the compiler going to know that? By predicting the future?
 
R

Roedy Green

As it stands, the code won't even compile, because a is never
defined anywhere. At run-time, it would be defined, but how the heck
is the compiler going to know that? By predicting the future?

What you could do though is write a new class with the additional code
and compile it on the fly, or generate the byte code for it on the
fly.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top