943,892 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 709
  • Java RSS
Sep 29th, 2008
0

please enlighten me on what i need to do to get this right

Expand Post »
Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class program1 {
  5. Scanner filescan = null;
  6.  
  7. public program1(){
  8. try {
  9. filescan = new Scanner (new File("t.txt"));
  10. } catch (FileNotFoundException fe){
  11. fe.printStackTrace();
  12. }
  13. }
  14. public static void main(String[] args)throws IOException {
  15.  
  16. printFile();
  17.  
  18. }
  19.  
  20. public static void printFile()throws IOException {
  21. int num;
  22.  
  23. while(filescan.hasNextInt()){
  24.  
  25. num= filescan.nextInt();
  26. System.out.print(num+" ");
  27. }
  28.  
  29. }
  30.  
  31. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 29th, 2008
1

Re: please enlighten me on what i need to do to get this right

So declare "filescan" static then.
Java Syntax (Toggle Plain Text)
  1. 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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 29th, 2008
0

Re: please enlighten me on what i need to do to get this right

Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class program1 {
  5. static Scanner filescan = null;
  6.  
  7. public program1(){
  8. try {
  9. filescan = new Scanner (new File("t.txt"));
  10. } catch (FileNotFoundException fe){
  11. fe.printStackTrace();
  12. }
  13. }
  14. public static void main(String[] args)throws IOException{
  15.  
  16. printFile();
  17.  
  18. }
  19.  
  20. public static void printFile()throws IOException{
  21.  
  22.  
  23. while(filescan.hasNextInt())
  24. System.out.println(filescan.nextInt());
  25.  
  26. }
  27.  
  28. }

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)
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 29th, 2008
0

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".
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 29th, 2008
0

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
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 29th, 2008
0

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?)
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 29th, 2008
0

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)

Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class program1 {
  5. static Scanner filescan = null;
  6.  
  7. public program1(){
  8. /*try {
  9. filescan = new Scanner (new File("t.txt"));
  10. } catch (FileNotFoundException fe){
  11. fe.printStackTrace();
  12. }*/
  13. }
  14. public static void main(String[] args){
  15.  
  16. try {
  17. filescan = new Scanner (new File("t.txt"));
  18. } catch (FileNotFoundException fe){
  19. fe.printStackTrace();
  20. }
  21.  
  22. printFile();
  23.  
  24. }
  25.  
  26. public static void printFile(){
  27.  
  28.  
  29. while(filescan.hasNextInt())
  30. System.out.println(filescan.nextInt());
  31.  
  32. }
  33.  
  34. }
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 29th, 2008
0

Re: please enlighten me on what i need to do to get this right

Click to Expand / Collapse  Quote originally posted by SyLk ...
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
java Syntax (Toggle Plain Text)
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. /** Not really a good descriptive class name, but I don't know
  7.  * what you're planning to do with it. You can pick a better one.
  8.  */
  9. public class FileProcessor {
  10.  
  11. private Scanner filescan = null;
  12.  
  13. /** Create an instance of FileProcessor for the given File. */
  14. public FileProcessor(File targetFile) throws FileNotFoundException {
  15. filescan = new Scanner(targetFile);
  16. }
  17.  
  18. /** Processing operations are carried out by calling
  19.   * public methods on an instance of this class.
  20.   */
  21. public void printFile() throws IOException {
  22. while (filescan.hasNextInt()) {
  23. System.out.println(filescan.nextInt());
  24. }
  25. }
  26.  
  27. /** main() can be used as a "driver" to manipulate an instance
  28.   * of this class. It should not do anything directly with the internal
  29.   * variables. All operations should be accessed only through
  30.   * methods provided by the class.
  31.   */
  32. public static void main(String[] args) {
  33. File targetFile = new File("t.txt");
  34. try {
  35. FileProcessor fileProcessor = new FileProcessor(targetFile);
  36. fileProcessor.printFile();
  37. } catch (FileNotFoundException fnf) {
  38. System.out.println("Target file: " + targetFile + " not found.");
  39. System.exit(1);
  40. } catch (IOException ex) {
  41. System.out.println("An IO error occurred processing this file");
  42. ex.printStackTrace();
  43. System.exit(1);
  44. }
  45. }
  46.  
  47. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 29th, 2008
0

Re: please enlighten me on what i need to do to get this right

Click to Expand / Collapse  Quote originally posted by SyLk ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

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.
Message:
Previous Thread in Java Forum Timeline: UDP packet
Next Thread in Java Forum Timeline: Need a little help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC