• 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

Question on String Tokenizer .

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody,
I have an application which has two parts. It gets employee data from the user, SSN, firstname, lastname and stores it in a text file.
This data is stored in the format:
SSN: FirstName: LastName.
Then the String Tokenizer needs to get the individual parts of this using the delimiter:.
I am pasting the three files.
1.Employee1 - writes to the file
2.Employee App - calls Employee1
3.Employee2 - reads from file
The reading part using the String Tokenizer is not working. It is directly going to the catch statement.
I will really appreciate it if you can help me with this.




Thanks
Pallavi
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's hard to know where to start with this program. I suppose "with the immediate cause of the error" is one place: you call the file "EmpDetails.txt" in one place, and "filename.txt" in another, and so presumably there's an exception when the file is opened for reading. You'd know that if in your catch block (ALL YOUR CATCH BLOCKS!) you actually displayed the exception object using printStackTrace(), or at least getMessage(); this is a good general policy, and something to always try before asking here.
One other random comment, and then I'll leave well enough alone: the constructor that does nothing but write a record to a file is REALLY gross: constructors are supposed to initialize objects, not do otherwise unrelated work.
[ July 14, 2003: Message edited by: Ernest Friedman-Hill ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pallavi,
StringTokenizer is part of the java.util.* class; you need to import java.util.*;
The code below compiles and runs...
import java.io.*;
import java.util.*;
public class Employee2 {
public static void main(String args[]) throws IOException {
try {
FileReader fr = new FileReader("filename.txt"); //creating a File Reader Object

BufferedReader br = new BufferedReader(fr);

while(br.ready()) {
String line = br.readLine();
StringTokenizer st = new StringTokenizer(line, ":");//creating a System Tokenizer.
int SSn = Integer.parseInt(st.nextToken().trim());//get the SSn part
String firstname = st.nextToken().trim();//get the firstname part
String lastname = st.nextToken().trim();//get the lastname part
System.out.println("SSN=" + SSn + " Fname=" + firstname + " Lname=" + lastname);
}//~while(br.ready())...

br.close();
}//~...

catch(Exception e) {
System.out.print("Error now");
}
}//~public static void main(...
}//~public class Employee2...
reply
    Bookmark Topic Watch Topic
  • New Topic