wood burning stoves
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

extracting strings

M Du

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
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,
file = "q5.dat",
final int INT = 11,
EXT = 22;
StringTokenizer tokens;
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...
}//end while
}// end try
catch (IOException e)
System.out.println("No File");
}// end catch
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:
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 ;
line = inFile.readLine();
}//end while

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

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
It's not a secret anymore!