• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading from file...

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the tutorial.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i figured out how to get just the employee number...


here's the code:

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic