This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Two scanner FileReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two scanner FileReader" Watch "Two scanner FileReader" New topic
Author

Two scanner FileReader

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I am able to compile my code but for some reason when I try to run my program nothing happens and I narrowed it down to the second scanner filereader that i need to read in data from a second file and create a customerList. I made it exactly the same as the first one so I don't know what i'm doing wrong. The only part that needs to be looked at is the first 30 lines but just in case I thought i'd send the rest.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Are you getting any exceptions? You can use a debugger to step through and see why this is happening.


Mohamed Sanaulla | My Blog
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

put debugging point and run code and find out where that your program giving exception.


Software Engineer(BSC):SCJP 1.5
(Knowledge is power when applied)
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I guess I don't understand how to put debugging point, is there a way I can see an example?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

You can use an IDE for that- You can download Netbeans and search for debugging using Netbeans.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I figured out how to use netbeans and the debugger and this is what i get

Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar
Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar
Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar
java.io.FileNotFoundException: customerDat.txt (No such file or directory)

I have the file in the same folder and project why does it say there is no such file or directory, the spelling is correct too, I checked it 3 times to be sure?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Just see if your code is using any packages and see where the file is located relative to the location of class from where you are using it.
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

Looking at your code I don't see any classes VideoList, CustomerList with default access. Where are those located? If they are in different packages they you should explicitly import them.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I am not familiar with these items having default access, I could very well have not added that at all. Is there a certain bit of code I should have added to make them default access?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Anton Shaykin wrote:Looking at your code I don't see any classes VideoList, CustomerList with default access. Where are those located? If they are in different packages they you should explicitly import them.


He has problem accessing an external file and not the Java class. Looking at the Exception- its FileNotFoundException.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Dustin Schreader wrote:I am not familiar with these items having default access, I could very well have not added that at all. Is there a certain bit of code I should have added to make them default access?


Where have you stored you other file? If there's is no FileNotFoundException for the other file, then you can place your customerDat.txt in the same location.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
Ok I started out making this on jpadpro and all the files, even the .txt files were in the same folder. I then started using netbeans and i'm sure that I selected the folder all the files were in. The weird part is just by changing some of the code around I can get the debugger to say that the other videoDat.txt file can't be found either. All the files are in the same folder so that should make them local or viewable i guess right?
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
Is there something wrong with the way I'm doing the Scanner and readfile, making it not able to see that I have data files? Or maybe some other flaw in the code or one of my other classes?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
You don't usually need to pass a FileReader to the Scanner constructor. You can if you want to because it implements the Readable interface, but you can usually pass a File object. But (as people have already told you) a FileNotFoundException can be caused by the file not being in the location you specified. It can also be caused because the file is read-only or write-only, or it is already in use by another application.

It is also worthwhile double-checking the exact spelling of the file names. If your file is actually called CustomerDat.txt, you will be unable to find it. If you really can't find the files, find out about JFileChooser to seek files.

To give a member of a class (or a class) default (or package-private) access, simply miss out the access modifier; don't call it public or private or protected.

As for other flaws, it worries me to see a main method 132 lines long. It also worries me to see all members of a class marked static, unless you are creating a "utility class", which I don't think you are.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
Thank you, I will try and go through my code to see what I can change and check the read write only. BTW it worries me too that the main method is that long and static but according to the assignment I have to use the authors code which Isn't the best to begin with and we have to modify it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
If you are given particular instructions for your assignment, you will have to go along with them. It may be worth telling your teacher that people have misgivings about that bit of design; he might use something different next year.
 
 
subject: Two scanner FileReader