This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Why I can't read text file in eclipse

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to read from a text file to input data to my java program. However, eclipse continuously gives me a Source not found error no matter where I put the file.I tried everything but without any success.Some of the things I did were to refresh the project ant to indicate the complete path to the file and also to move the file throughout all possibles directories of the project.I also tried to compile the file in Netbeans and got the same result.It is not possible to have a code error because it's about sample programs from Professor Barry Bird's book Java for Dummies.For example sample code 8.01,10.01 and 10.04. Programs from the same book that do not have a text file work without problems.My operating system is Windows 10 64 bits and my computer is HP, if it has any importance.When I try to compile the code, I get generally the following errors:
       
       at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at DoPayroll.payOneEmployee(DoPayroll.java:22)
at DoPayroll.main(DoPayroll.java:13)

I would be very grateful if someone could help me to solve the problem.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alexandar, welcome to the Ranch!

When you post here and you have a question about your code, it helps a lot if you post that code so that we can see it. Also if you're asking about an exception it helps if you post the stack trace. You did post part of the stack trace, but you left off the beginning, which has important information.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Source not found" probably mean you haven't got the file in the right place.

Instead of trying to guess where it should go, get the program to tell you.
If you have a File object, then you can use the getAbsolutePath() method to tell you exactly where java will go looking for this file.
If it is Eclipse, the file will probably be relative to your Eclipse project.

But I am just guessing here - as Paul said it would be helpful if you showed us what code you have, what file you are looking for, and explicitly what exception is thrown
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksandar Cvetanov wrote:When I try to compile the code, I get generally the following errors:
       
       at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at DoPayroll.payOneEmployee(DoPayroll.java:22)
at DoPayroll.main(DoPayroll.java:13)


Another thing to post is the complete stack trace (error message).  Right now I can see where the problem originated , but not the error.  The "Unknown Source" is not generally the problem.

However, if the system can't find something as basic as "java.util.Scanner", it may be that your Java environment is not correctly configured.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error exactly do you get?
The stack trace you posted earlier is only part of the full error message.

This is on the assumption that the code compiles.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing, what does the file look like that you are reading in?

Because it looks like it is failing to read in data, which (if we had the full error text) I'm guessing is it's run out of data.
 
Aleksandar Cvetanov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file is a normal text file an it looks like this:

Barry Burd
CEO
5000.00
Harriet Ritter
Captain
7000.00
Your Name Here
Honorary Exec of the Day
10000.00

I left the cursor beneath the last number according to Barry Burd's instructions for creating text files.
As far as mistakes are concerned, everything that appears on the screen is what I mentioned above.
If it helps,here you have the errors that come up when compiling the same code with Netbeans:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at DoPayroll.payOneEmployee(DoPayroll.java:22)
at DoPayroll.main(DoPayroll.java:13)

 
Aleksandar Cvetanov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoever wants to test the code can download the sample code from here: https://users.drew.edu/bburd/JavaForDummies6/ and open the listing 08-01 or other prog with text file
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested this with the code you posted and the file in the project folder and it worked fine.
 
Bartender
Posts: 4682
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could it be that your Locale has a comma as decimal separator?
 
Aleksandar Cvetanov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The truth is, I tried these programs earlier when I was with Windows 7 and Toshiba laptop and they worked normally. I do not know if the problem is in the computer or in the operating system. I've already reinstalled Java and Eclipse several times.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you want to bet the input file doesn't have a blank line at the end?
 
Aleksandar Cvetanov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I install Java and Eclipses on my old 32-bit Windows 7 laptop everything works normally. I will leave things as they are and thank you all for your cooperation.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the input file on the old computer have a blank line at the end?  Is it different than the one on the new computer?
 
Aleksandar Cvetanov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,it has a blank line on the end.I have explained this above in the thread.It is clear that Java has its flaws when the same program runs differently on different computers.I am not the only one with this problem. I have searched with Google and I have found that the same thing happens to other people.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in order to have eclipse read text files in the IDE you have to go into the settings and tell eclipse to associate text files so they open up in the code window. If you're trying to read in a text file through your program try youtube it has many examples here is one https://www.youtube.com/watch?v=BxCbxfpwC7Q
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic