This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am not familiar with the readUTF() methods, but look in DataInput, and you find it doesn't take parameters. You will have to go through the implementing classes; I think what you are doing is setting up some sort of input stream, which takes the name of the file as a parameter to its constructor. Then I think the readUTF() goes through the file already set up, reading whatever it finds assuming it is in UTF format.
Get rid of all "static"s except in "public static void main(String args)"
Move "String text = ""; String path = "E:\\etc\\etc":" out of the main method as fields.
Change "if(args.length > 0)" to "else"
Move the bit about setting up text into a constructor which takes String args as its parameter, and pass args to that constructor.
Move everything in the main method following this point. Most of it can be put into a new method, let's call it "go()"[LIST]I tried it and got a console printout saying "unable to find file."
Look here in the Java Tutorial. It says a bit about static members. In Java we don't say dynamic, even though the origin of the word "static" is from "static or dynamic." We say "instance" and the compiler says "non-static." Static members have the following characteristics:-
Static members are loaded into memory when the class is loaded at execution time, and stay put until (as far as I know), execution finishes.
Instance (non-static) members are loaded into memory when an object is created, and removed from memory when that object is no longer referenced and is deleted by the garbage collector. You could call this "dynamic memory management."
The static members are loaded once and once only; there is only one copy per class per JVM.
Static members are loaded into memory and accessible before an instance of that class is created.
Since there can be several instances of any class, one can never tell from a static context which instance one is trying to gain access to. So the compiler prohibits access from a static member to an instance member.
Access from an instance member of an object to static members of the same class is always permissible, but it is always the same static member (there is only one of each static member in memory).
This same static member is "shared" between all instances; if one instance alters it, all other instances find the same alteration.
From outside the class one ought not to say myObject.myStaticMember, but myClass.myStaticMember, using the name of the class. [This rule is not strictly enforced.] Call static members by the name of their class.
Beware: If you program in other languages, they have different keywords; the keyword "static" is used in C too, but it means something different. In C# it means more-or-less the same as in Java.
Hope this helps
minor spelling and formatting corrrections[/edit] [ April 11, 2007: Message edited by: Campbell Ritchie ]
Joined: Oct 13, 2005
As it says in the Java Tutorial reference I gave you, you cannot use the keyword "this" in a static context (nor the keyword "super," I think).
Look at the Math. class, which has methods which take information, calculate an answer and return it, without altering anything else in the class (eg Math.sin(PI / 2.0) gives the return 1.0 and doesn't alter anything else). If you find yourself with methods which do neither alter any information in an object, nor take any information from an object and manipulate or return it, then it is probably best to declare such methods static.
Change PI to PI / 2.0[/edit] [ April 11, 2007: Message edited by: Campbell Ritchie ]
Joined: Apr 10, 2007
Most of what you've said i agree with and i should have perhaps explained somethings.
As stated in my first post, I am taking a simple task of having a console program start with a parameter OR not.
1. run program and display text (if arguments where given) to the screen and exit. done and moved on
2. add a test to check for argument and if nothing display suitable message, then exit. Or display the argument that was given to the screen. Done and moved on
3. use a method to do the above (namely a method to display to the screen). Done and moved on
4. introduce file handling, test for the filename is present, if it is delete it, then create it again inserting the argument passed (write the argument to a file in a known location and filename), then read from that file the data inside to the screen, then exit. hit a snag with i/o operation (see erros at end of post)
5. look at refining the program more (ie introduction of constructors etc). to be done
all of this was so i could take baby steps to understanding how things glue together.
I have a book here by Ivor Hilton on Beginning Java 2. Which has helped me in some understanding (and lost me on others - ps got up to end of chapter 5 defining classes then i start going glassy eyed so to speak )
Found a tutorial that gave me this idea of this program as i kept giving up because i couldn't really relate what i was reading etc to something pratical.
So here is the Original program i had along with the errors i got notice they all refer to the fact that te datastream "variables" for reading and writing are te issue.
Errors i got generated when i compile (currently under linux but also under Windows too
dave@localhost:~$ /home/dave/jdk1.6.0/bin/javac /dos/my_project/crm/mysimpleio.java /dos/my_project/crm/mysimpleio.java:41: non-static method ToFile(java.lang.String) cannot be referenced from a static context ToFile(text); ^ /dos/my_project/crm/mysimpleio.java:56: non-static method FromFile(java.lang.String) cannot be referenced from a static context FromFile(text); ^ /dos/my_project/crm/mysimpleio.java:64: cannot find symbol symbol : variable thefileOut location: class mysimpleio thefileOut.write(thefilename); ^ /dos/my_project/crm/mysimpleio.java:67: cannot find symbol symbol : variable thefileOut location: class mysimpleio thefileOut.close(); ^ /dos/my_project/crm/mysimpleio.java:73: cannot find symbol symbol : variable thefileIn location: class mysimpleio thefileIn.read(thefilename); ^ /dos/my_project/crm/mysimpleio.java:75: cannot find symbol symbol : variable thefileIn location: class mysimpleio thefileIn.close(); ^ 6 errors dave@localhost:~$
under linux i'm using Java 1.6.0 and windows it's 1.5
please if you can assist me with the I/O issue i think i can get the constructor bit okay (well i did so when working from the book and erros i got were mostly typo's or of that ilk.
Should i move the datastream bits down into the methods and not in the main as they are currently??
ps appreachiate the hints about moving code out to make it cleaner.
Joined: Oct 13, 2005
I dislike books which put some much into the main method; I don't think this is good object-orientation, despite examples seen in books (and even the Java Tutorial ).
Set up an instance method to start your application off, let's call it statReading(). Reduce your main method to the bare minimum, like this:-Move everything else you have in the main method at present into the startReading() method, except for anything required to set up the initial state of the object, which goes in the constructor.
BTW: You are using the wrong conventions for names of classes. Use System.err rather than System.out for messages in catch blocks.
If you are at such an early stage in your programming, you might prefer to try a few easier examples before going on to file reading and writing.