Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save as code

Status
Not open for further replies.

Coder7

Programmer
Oct 29, 2002
224
US
Hi there...

Is there a way of saving user input as code? A very trivial example just for demonstration follows :

If I initially wrote a program that when executed and the user clicks on a button, a text box prints out the famous message "Hello there". Say the user wants to change this message to "Hello Nerd!" and I included another text box for the user's input. So, is it possible to write code such that when the user enters "Hello Nerd!" in the input text box and clicks enter, the program would take the user's input as code and store it between the actual coding lines? i.e when the user exits the program and reloads it again another times and clicks on the same old button, the message "Hello Nerd!" is printed instead!

Thanks a lot!!
 
Hi Coder7,

You can't write directly to class-files. If you don't want to use text files (This could be done f.ex. by using message=hello... line in some properties file), you can use object serialization. So you save the state of the object, it allows you to do easily user-specific settings... but this is not exactly what you wanted, i guess...

Here is some example if you need one:
(It's not very good, but you can serialize every object
that implements Serializable interface, included GUI-components.)
________________________________________________________
import java.io.*;

public class Main
{
public static void main(String[] argv) throws Exception
{
new Main();
}

public Main() throws Exception
{

File f = new File("ownObj");
if(!f.exists()) // No serialized obj. found
{
SerTest st = new SerTest();
System.out.println(st.getMessage());
System.out.print("Give a new welcome message: ");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st.setMsg(br.readLine());

ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(f));
os.writeObject(st);
os.flush();
os.close();
}
else // deserialize SerTest obj.
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
SerTest st = (SerTest)ois.readObject();
ois.close();

System.out.println(st.getMessage());
}
}

}

_______________________________________________________

port java.io.*;

public class SerTest implements Serializable
{
public SerTest()
{
_msg = "Hello there!";
}

public void setMsg(String msg_)
{
_msg = msg_;
}


public String getMessage()
{
return _msg;
}

private String _msg;
}

_________________________________________________________

That was what came to my mind, maybe someone else knows better, LeonTang maybe?
 
Erm... I actually didn't see the need to reply to the post since Vepo is correct that it is not possible unless I write the String to a file or database etc. However, there is still a way but isn't good at all (I am not sure whether it will works or not too).

Create 1 more method in your class. Everytime before exiting your program, call this method. What this method do is to write to the java file (not appending) the entire code again (you can hardcode the entire code except for the part where you want the message to be different) and then using the method exec() in Runtime, compile the program.

Hope this helps,
Leon

If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top