wood burning stoves 2.0*
The moose likes Java in General and the fly likes extracting strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "extracting strings" Watch "extracting strings" New topic
Author

extracting strings

M Du
Greenhorn

Joined: Nov 21, 2002
Posts: 2
theres an assignment question that i cant seem to get working. can someone help me. basically fill in the blank lines.
What i am required is to extract the dat file and create ID's for these students. so the output should look like:
99110289 Smith, Daniels
The values derive from the following positions INT=11, EXT=22.
1999/INT/05-02-89/Smith, Daniels
99 got from the year
INT=11
02-89 dateOfBirth
the q5.dat file is:
1999/INT/05-02-89/Smith, Daniels
2000/EXT/01-12-90/Thompson, Bill
2001/INT/21-11-80/Lake, Trevor
2002/INT/13-09-79/Fry, John

import java.io.*;
import java.util.StringTokenizer;
public class Question5
{
public static void main (String [] args)
{
int totalEnrolments = 0;
String line,
enrolmentYear,
file = "q5.dat",
dateOfBirth,
studentID,
status;
final int INT = 11,
EXT = 22;
StringTokenizer tokens;
try
{
FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);
line = inFile.readLine();
while (line != null)
{
//i have to do something here. something like extracting substrings...
System.out.println(studentID);
}//end while
inFile.close();
}// end try
catch (IOException e)
{
System.out.println("No File");
}// end catch
System.out.println();
System.out.println("Total number of enrolments: " + totalEnrolments);
}//end method
}end class

[ November 21, 2002: Message edited by: M Du ]
Fahd Shariff
Ranch Hand

Joined: Nov 22, 2002
Posts: 38
try this:
<snip>
while (line != null)
{
//i have to do something here. something like extracting substrings...
tokens = new StringTokenizer(line,"/");
String year = tokens.nextToken() ;
String type = tokens.nextToken() ;
if(type.equals("INT")) type = "11" ;
else type = "22";
String dob = tokens.nextToken() ;
String name = tokens.nextToken();

String id = ""+year.charAt(2)+year.charAt(3)+ type+dob.substring(3,5)+dob.substring(6,8)+' '+name ;
System.out.println(id);
line = inFile.readLine();
}//end while
</snip>


Fahd Shariff<br />"Let the code do the talking"
M Du
Greenhorn

Joined: Nov 21, 2002
Posts: 2
thanx heaps.
i guess i gotta add more varibles.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extracting strings