Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can I solve this Puzzle???Java String

Reply
Thread Tools

How can I solve this Puzzle???Java String

 
 
heinz
Guest
Posts: n/a
 
      06-19-2004
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-19-2004
On 18 Jun 2004 17:40:44 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (heinz) wrote or
quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      06-19-2004
Roedy Green <(E-Mail Removed)> scribbled the following:
> On 18 Jun 2004 17:40:44 -0700, (E-Mail Removed) (heinz) wrote or
> quoted :


>>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.


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.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-19-2004
On 19 Jun 2004 09:00:27 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

>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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      06-19-2004
Roedy Green <(E-Mail Removed)> scribbled the following:
> On 19 Jun 2004 09:00:27 GMT, Joona I Palaste <(E-Mail Removed)>
> wrote or quoted :
>>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


Yes, it's certainly possible to make Java programs out of text. We
wouldn't be here otherwise. But I gather that what the OP wants to do is
modify the code of the *CURRENTLY EXECUTING PROGRAM* based on the text
he read from the file.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-19-2004
heinz wrote:
> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-19-2004
On 19 Jun 2004 16:27:40 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

>Yes, it's certainly possible to make Java programs out of text. We
>wouldn't be here otherwise. But I gather that what the OP wants to do is
>modify the code of the *CURRENTLY EXECUTING PROGRAM* based on the text
>he read from the file.


that's what the eval entry is about.

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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      06-19-2004
Roedy Green <(E-Mail Removed)> scribbled the following:
> On 19 Jun 2004 16:27:40 GMT, Joona I Palaste <(E-Mail Removed)>
> wrote or quoted :
>>Yes, it's certainly possible to make Java programs out of text. We
>>wouldn't be here otherwise. But I gather that what the OP wants to do is
>>modify the code of the *CURRENTLY EXECUTING PROGRAM* based on the text
>>he read from the file.


> that's what the eval entry is about.


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


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?

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The truth is out there, man! Way out there!"
- Professor Ashfield
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-19-2004
On 19 Jun 2004 19:34:57 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

>
>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-19-2004
On 19 Jun 2004 19:34:57 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

>AFAIK that still doesn't count as modifying the code of the currently
>executing program.


Another possible approach is to use a scripting language like JPython.

See http://mindprod.com/jgloss/jpython.html

I don't yet understand the problem, so I can only shoot potential
general solutions trusting OP can read them and take what is of use.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help me to solve this problem,binding a String object to a string preethamkumarrai@gmail.com Java 5 01-29-2006 03:32 PM
If you can solve this... James Wireless Networking 3 10-23-2005 01:00 AM
how can I solve Common Language runtime Error Ajith Nair ASP .Net 0 08-17-2005 08:17 AM
How can i solve this problem? Miguel Dias Moura ASP .Net 9 12-10-2004 07:12 PM
anyone can solve my issue Abdul Cisco 0 08-02-2004 06:25 AM



Advertisments