End of year Java Project Question - File I/O
Hello.
My project involves allowing the user (which should be someone learning the very basics of Java and OOP) to copy and paste a one class program into a JTextArea. Then, they click a button which analyzes their code and visually shows how objects in their program interact with one another. My problem is that I am not sure of how to GET their code after it is pasted. I read up on FileReader/Writer but it seems like they save .txt files; I do not need to save the user's program on disk but only in memory and analyze their. Does anyone know of how I could do this? I obviously do not want to save it as a String because if the code is 100 lines long, then it just gets too messy. I would like to be able to read line by line what the user is trying to accomplish in their program in a txt file. If I could save a txt file and at close of the program, delete it, that could work too.
Any help? Thanks in advance.
Sounds like a pretty ambitious project. To start, JTextArea has a method 'getText()' which will grab all the text in the text-area. After the code is retrieved it can be parsed out however desired.Writing that accumulated text to a file afterward should not a problem.
> Does anyone know of how I could do this? I obviously do not want to
> save it as a String because if the code is 100 lines long, then it just
> gets too messy. I would like to be able to read line by line what the
> user is trying to accomplish in their program in a txt file. If I could save
> a txt file and at close of the program, delete it, that could work too.
huh? you can write a whole application on one line
for (int i = 0 ; i < 10; i++){ System.out.println("Line 1"); System.out.println("line 2"); etc... }
This is ambitious for an end of class project (a class project even)
your application have to check the syntax, java keyword, keep what object is instantiate, what method is invoked..if that method belongs to the class...etc..
Well, the purpose of this program is to help my teacher show his future students how things really work.
For example, when I started out, I had a problem understanding OOP (super classes, subclasses, inheritance, polymorphism or dynamic bynding) and I figured, whenever my teacher writes one of his programs that compiles correctly and correctly runs, that would mean that his syntax is correct and could show his students how the program creates, trashes, adds to ArrayLists, whatever. Yes, this is an ambitious project, but it does not matter if I finish now or weeks/months from now, I would just like to get this useful project done.