![]() |
| ||
| please enlighten me on what i need to do to get this right import java.util.Scanner; 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 |
| ||
| Re: please enlighten me on what i need to do to get this right So declare "filescan" static then. static Scanner filescan = null;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. |
| ||
| Re: please enlighten me on what i need to do to get this right import java.util.Scanner; 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 Move the scanner initialization into main() then, since you don't actually construct an instance of "program1". |
| ||
| 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. Thanks guys |
| ||
| Re: please enlighten me on what i need to do to get this right Well, since you didn't state the runtime errors, I'm going to guess they stem from not being able to locate the file? (Just an aside, never throw exceptions from main(). What is going to catch them?) |
| ||
| 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) import java.util.Scanner; |
| ||
| Re: please enlighten me on what i need to do to get this right Quote:
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 import java.io.File; |
| ||
| Re: please enlighten me on what i need to do to get this right Quote:
|
| All times are GMT -4. The time now is 6:03 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC