?
Solved

How to parse file string with initial parameter

Posted on 2003-03-13
19
Medium Priority
?
212 Views
Last Modified: 2010-03-31
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
Comment
Question by:cikcik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 4
19 Comments
 
LVL 2

Expert Comment

by:functionpointer
ID: 8133922
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
 

Author Comment

by:cikcik
ID: 8133949
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8133965
Are you saying that not all lines of the file contain initial codes?
FMI, your reading the file with BufferedReader, right?
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:cikcik
ID: 8133987
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
 

Author Comment

by:cikcik
ID: 8134002
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8134016
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
 

Author Comment

by:cikcik
ID: 8134026
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
 
LVL 2

Accepted Solution

by:
functionpointer earned 100 total points
ID: 8134074
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
 

Author Comment

by:cikcik
ID: 8134085
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
 

Author Comment

by:cikcik
ID: 8134105
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8134113
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8134132
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
 

Author Comment

by:cikcik
ID: 8134136
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8134140
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
 

Author Comment

by:cikcik
ID: 8134186
thx functionpointer.

I will test your code, and give the result as soon as possible.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8134203
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8134210
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
 

Author Comment

by:cikcik
ID: 8134982
Thx Pointer...
Thx Mayank...
Your answer really helpful.

I accept pointer who first answer me.
0
 

Author Comment

by:cikcik
ID: 8134988
Thx for your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question