Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extracting strings

 
M Du
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
M Du
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx heaps.
i guess i gotta add more varibles.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic