Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading from file...  RSS feed

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to i go about reading from a text file....

cause i have to read from a text file, which has employees and more info,

and i have to load these characteristics into a doubly linked list..

ive seen the method implemented using the Scanner class, but do i have to

import

java.util.Scanner;
java.io.*



??


thx,
Justin
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aunt Google is your friend. When in doubt, ask her. Here is what she has found for you:

http://java.sun.com/developer/JDCTechTips/2004/tt1201.html
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/scannerConsole.html
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i figured out how to read and write to files....

but here is my code.



and here is the output:



now if i just put,

answer = scan.next();

it just gets the number, then the employee, etc..

not the whole line..

so how do i go about getting just the employee number, then go to the

next line???

thx,
Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i figured out how to get just the employee number...


here's the code:

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, but from the output from the first example, i also need to get only the wage... which is the last column... is there a scan.last() ? ... or can i do scan.next().next().next()??? like a doubly linked list?


Take a peek at the JavaDoc for the Scanner class. It supports regular expressions -- the findInLine() and match() methods should grab what you want.

Don't have any examples though -- as all the projects that I am currently working on, are using Java 1.4.

Henry
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: You ought to have been told, you can get potential problems if you open a file and don't close it. You have to put the close bit in a finally block; then even if there is an Exception, it is still executed.

But, the close() method might throw an Exception, so it has to be inside a try block as well.



The best answer is usually:-That way you make absolutely sure that whatever file you opened is closed again.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I'm misunderstanding your requirements but why don't you parse each line into an Employee instance, and put that in the list, and then ask each instance what its name/wage/id etc.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!