Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

How to parse file string with initial parameter

Hi Friends..
I have a data file like below this :

M101ANDREAS/VICKY MS
M201ADT88915095354
M3011 0RR06AUGAIRNSINSINGAPORE

I want to divide this data by Initial Code (e.g.M1,M2,M3)
then by char index.. e.g:
M101ANDREAS/VICKY MS
M1 is initial code
01 is item number
and next is another data.

Could you help me?

Thx.
0
cikcik
Asked:
cikcik
  • 10
  • 5
  • 4
1 Solution
 
functionpointerCommented:
if you know the exact indexes of the data you want, why don't you just:
String s = "M101ANDREAS/VICKY MS";
String initialCode = s.substring(0,2);
String itemNumber = s.substring(2,4);
String anotherData = s.substring(4);
?
0
 
cikcikAuthor Commented:
Thx for your question.
I know I have to use substring.
But I'm not sure how to find the line of file that have initial code like M1, M2, M3.
Because the line not fixed.
Example 1:
M101
M102
M201
M202
M301
M302

Example 2 :
M101
M201
M301

How about the complete code?
0
 
functionpointerCommented:
Are you saying that not all lines of the file contain initial codes?
FMI, your reading the file with BufferedReader, right?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cikcikAuthor Commented:
Thx for your question.
I know I have to use substring.
But I'm not sure how to find the line of file that have initial code like M1, M2, M3.
Because the line not fixed.
Example 1:
M101
M102
M201
M202
M301
M302

Example 2 :
M101
M201
M301

How about the complete code?
0
 
cikcikAuthor Commented:
yes not all line of the file contain initial codes.
there's can be new line between initial code. And there's can be the line without initial code.

ex:
M101
12345

M201

M301

M302

So I have to find the right line.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Please be a little more specific about how many letters, etc can the code, item number extend to and I'm sure we can help you out. Generally, item numbers, etc have standard length and in case two items have numbers 100 and 11, its better to write 100 and 011 - that standardizes everything. The same goes for the code also. They should have standard length, prefixed by zeroes if required.

Anyway, if you can tell us a little more (like maximum length of code, etc), we can definitely help you out.

Also, your third example:

>> M3011 0RR06AUGAIRNSINSINGAPORE

There is a blank here (which is not there in the other examples that you've given) and there's a zero after that blank. If this example is correct, what do you want to be the item-code, item-number, etc?

Mayank.
0
 
cikcikAuthor Commented:
yes not all line of the file contain initial codes.
there's can be new line between initial code. And there's can be the line without initial code.

ex:
M101
12345

M201

M301

M302

So I have to find the right line.
0
 
functionpointerCommented:
hmmm..  This will work for the example you posted. (Assuming you only want to capture complete records. Actually, this assumes quite a bit) :)

Vector v = new Vector();

String initialCode, itemNumber, anotherData, line;

while ( ( line = br.readLine() ) != null )
{
  if ( initialCode == null && line.startsWith( "M" ) )
  {
    initialCode = line.substring(0,2);
    itemNumber = line.substring(2,4);
  }
  else if ( line.length() > 0 )
  {
    anotherData = line;
    // now do something with initialCode, itemNumber, and anotherData...
    v.addElement( new YourDataObject( initialCode, itemNumber, anotherData );

    initialCode = null;
  }
  else initialCode = null; // didn't contain data, move on.
  // or if you want to keep looking across empty lines, dont set to null.
}

// now you have a full Vector of YourDataObjects...
0
 
cikcikAuthor Commented:
Thx Mayank,

The initial Code can be extend from M0 to M9 (and not of the code must be exist... etc. M1, M2, M4 or M1,M2,M3, M6.
So I have to recognice the initial code.

The item number have 2 char length from 0-99.

Every Code have standard length.
etc.
M101ANDREAS/VICKY MS is 2 char for initial code (M2), 2 char for item number (01), 64 char for name with format : Surname/FirstName Title (ANDREAS/VICKY MS).

M201ADT88915095354 is 2 char for initial code (M2), 2 char for item number(01), 3 char for person type (ADT = Adult), 11 char for person number (88915095354).

M3011 0RR06AUGAIRNSINSINGAPORE is 2 char for initial code (M3), 2 char for item number(01), 1 char for product code (1), 1 char for link code (can be blank, in this example is blank), 1 char for control data (0),2 char for status code (RR), 5 char for date (06AUG), 3 char for product code (AIR), 1 char for request (N), 3 char for city code (SIN), 17 char for city name.

May be this explanation more clearer.

Thx for your help
0
 
cikcikAuthor Commented:
Thx Mayank,

The initial Code can be extend from M0 to M9 (and not of the code must be exist... etc. M1, M2, M4 or M1,M2,M3, M6.
So I have to recognice the initial code.

The item number have 2 char length from 0-99.

Every Code have standard length.
etc.
M101ANDREAS/VICKY MS is 2 char for initial code (M2), 2 char for item number (01), 64 char for name with format : Surname/FirstName Title (ANDREAS/VICKY MS).

M201ADT88915095354 is 2 char for initial code (M2), 2 char for item number(01), 3 char for person type (ADT = Adult), 11 char for person number (88915095354).

M3011 0RR06AUGAIRNSINSINGAPORE is 2 char for initial code (M3), 2 char for item number(01), 1 char for product code (1), 1 char for link code (can be blank, in this example is blank), 1 char for control data (0),2 char for status code (RR), 5 char for date (06AUG), 3 char for product code (AIR), 1 char for request (N), 3 char for city code (SIN), 17 char for city name.

May be this explanation more clearer.

Thx for your help
0
 
Mayank SAssociate Director - Product EngineeringCommented:
It is clearer but these three strings themselves have different formats, so are they in different files and supposed to be processed separately, or are they in the same file, supposed to be processed together?

Mayank.
0
 
functionpointerCommented:
you have consistancies in the structure of your data around the initial code an item number. Those are ALWAYS dealt with the same way.

If you like, once you determine the initial code and the item number, you can branch your logic based on these and handle the rest of the data differently. From the example i posted, change:

else if ( line.length() > 0 )
 {
   anotherData = line;

   AnotherDataItem adi = null;

   // now switch off initialCode or itemNumber
   // and make an object that implements AnotherDataItem
   int x = Integer.parseInteger( initialCode.substring(0,1) );
   switch ( x )
   {
     case 1: // do the name thing with anotherData
     case 2: // do the person type thing anotherData
     case 3: // do this bizarre thing anotherData
   }

   v.addElement( new YourDataObject( initialCode, itemNumber, adi );

   initialCode = null;
 }
0
 
cikcikAuthor Commented:
They are in one files and suppossed to be processed together.
They are one customer data with explained by initial code for the detail.
0
 
functionpointerCommented:
this is actually pretty common. legacy apps loved to jam as much data in a file as they could and use control bytes to determine the data type.
0
 
cikcikAuthor Commented:
thx functionpointer.

I will test your code, and give the result as soon as possible.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
OK.

Suppose you have a BufferedReader object to read the file:

String str, temp ;
int code, i, j ;
String itemCode, itemNumber, surName, firstName, title ;

str = br.readLine () ;
temp = str.charAt ( 1 ) ; // 0, 1, ...., 9
code = Integer.parseInt ( temp ) ;


switch ( code )
{
  case 0: // do your thing
          break ;

  case 1: itemCode = str.substring ( 0, 2 ) ;
          itemNumber = str.substring ( 2, 4 ) ;
          i = str.indexOf ( '/' ) ;
          surName = str.substring ( 4, i ) ;
          j = str.indexOf ( ' ' ) ;
          firsName = str.substring ( i + 1, j ) ;
          title = str.substring ( j + 1 ) ; // to the end of the string
          break ;

....
....
// sameway, do it for other cases

} // end of switch


Mayank.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
OK.

Suppose you have a BufferedReader object to read the file:

String str, temp ;
int code, i, j ;
String itemCode, itemNumber, surName, firstName, title ;

str = br.readLine () ;
temp = str.charAt ( 1 ) ; // 0, 1, ...., 9
code = Integer.parseInt ( temp ) ;


switch ( code )
{
  case 0: // do your thing
          break ;

  case 1: itemCode = str.substring ( 0, 2 ) ;
          itemNumber = str.substring ( 2, 4 ) ;
          i = str.indexOf ( '/' ) ;
          surName = str.substring ( 4, i ) ;
          j = str.indexOf ( ' ' ) ;
          firsName = str.substring ( i + 1, j ) ;
          title = str.substring ( j + 1 ) ; // to the end of the string
          break ;

....
....
// sameway, do it for other cases

} // end of switch


Mayank.
0
 
cikcikAuthor Commented:
Thx Pointer...
Thx Mayank...
Your answer really helpful.

I accept pointer who first answer me.
0
 
cikcikAuthor Commented:
Thx for your help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 10
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now