I've been battling with tryin to figure out how to use the scanner object "filescan" througout all my functions. can someone please direct on how to do this. im sure im not too far from getting it right.
thanks
when i attempt top use "filesan" in the print file method i get ------->
Cannot make a static reference to the non-static field filescan
but i need this method to be static so that i may call it in the main?
not sure on what needs to be done at this point
It's worth noting that making everything static and calling it from main() is not how Java is meant to be used, though intro courses often start with such programs. Java is an object-oriented language and should be taught as such. Making it all static relegates it to a procedural program and defeats the whole purpose.
I have tried this and i get -------->
Exception in thread "main" java.lang.NullPointerException
at program1.printFile(program1.java:23)
at program1.main(program1.java:16)
Re: please enlighten me on what i need to do to get this right
I've just attempted to move the scanner initialization to the main and i get more runtime errors. also if i did contruct an instance of program1 would that solve my issues. I really would like to get this right and understand it as all my projects from here on are going to require me to manipulate data from a single file as input for the methods which will all be called from the the main(). This is what the instructor requires.
Whether or not its best practice is a totally different topic.
Re: please enlighten me on what i need to do to get this right
these are the errors i have gotten now sir ( after removing exceptions)
java.io.FileNotFoundException: t.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at program1.main(program1.java:17)
Exception in thread "main" java.lang.NullPointerException
at program1.printFile(program1.java:29)
at program1.main(program1.java:22)
I really would like to get this right and understand it as all my projects from here on are going to require me to manipulate data from a single file as input for the methods which will all be called from the the main(). This is what the instructor requires.
Whether or not its best practice is a totally different topic.
Actually, "best practice" doesn't really come into it. It's the intended usage of the language. Making everything static and manipulating it in main() could only be described as "unintended practice" or more appropriately "worst practice". Java is entirely an object-oriented language. To teach it otherwise is a disservice to students.
Your code can easily be converted to an appropriate object-oriented version with a few simple changes. The only static method that is needed here is main(). Any others can be normal public methods of the class. All variables needed internally by the class should be declared private. The static main() method simply serves as entry point or driver to "set up" and use the object that you create, like so
these are the errors i have gotten now sir ( after removing exceptions)
java.io.FileNotFoundException: t.txt (The system cannot find the file specified)
Most likely the file you are trying to read is not located in the same directory as your java class. You can either move it there, or supply the full path name to the file in your program.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.