How can I solve this Puzzle???Java String

Discussion in 'Java' started by heinz, Jun 19, 2004.

  1. heinz

    heinz Guest

    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
     
    heinz, Jun 19, 2004
    #1
    1. Advertising

  2. heinz

    Roedy Green Guest

    On 18 Jun 2004 17:40:44 -0700, (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.
     
    Roedy Green, Jun 19, 2004
    #2
    1. Advertising

  3. Roedy Green <> scribbled the following:
    > On 18 Jun 2004 17:40:44 -0700, (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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
     
    Joona I Palaste, Jun 19, 2004
    #3
  4. heinz

    Roedy Green Guest

    On 19 Jun 2004 09:00:27 GMT, Joona I Palaste <>
    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.
     
    Roedy Green, Jun 19, 2004
    #4
  5. Roedy Green <> scribbled the following:
    > On 19 Jun 2004 09:00:27 GMT, Joona I Palaste <>
    > 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The trouble with the French is they don't have a word for entrepreneur."
    - George Bush
     
    Joona I Palaste, Jun 19, 2004
    #5
  6. heinz

    Chris Smith Guest

    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
     
    Chris Smith, Jun 19, 2004
    #6
  7. heinz

    Roedy Green Guest

    On 19 Jun 2004 16:27:40 GMT, Joona I Palaste <>
    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.
     
    Roedy Green, Jun 19, 2004
    #7
  8. Roedy Green <> scribbled the following:
    > On 19 Jun 2004 16:27:40 GMT, Joona I Palaste <>
    > 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The truth is out there, man! Way out there!"
    - Professor Ashfield
     
    Joona I Palaste, Jun 19, 2004
    #8
  9. heinz

    Roedy Green Guest

    On 19 Jun 2004 19:34:57 GMT, Joona I Palaste <>
    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.
     
    Roedy Green, Jun 19, 2004
    #9
  10. heinz

    Roedy Green Guest

    On 19 Jun 2004 19:34:57 GMT, Joona I Palaste <>
    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.
     
    Roedy Green, Jun 19, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    3,015
    Thomas Hawtin
    Jan 29, 2006
  2. Replies:
    2
    Views:
    360
    Mike Wahler
    Jan 31, 2005
  3. j vickroy

    map float string '0.0' puzzle

    j vickroy, Jan 27, 2004, in forum: Python
    Replies:
    2
    Views:
    313
    David M. Cooke
    Jan 27, 2004
  4. Alf P. Steinbach

    Ways to solve a puzzle in C++ -- variety

    Alf P. Steinbach, Oct 10, 2005, in forum: C++
    Replies:
    27
    Views:
    1,339
    Branimir Maksimovic
    Oct 13, 2005
  5. Roedy Green

    JNI Unicode String puzzle

    Roedy Green, Dec 18, 2007, in forum: Java
    Replies:
    7
    Views:
    937
Loading...

Share This Page