Text file parsing and displayin

hi i have a text file as follows

<s>
a=xyz
b=yzc
c=pqr
<e>
<s>
a=123
b=456
c=789
<e>


how can i get values and display them in a file. I want to display on a webpage/file following from the file

xyz
yzc
pqr

123
456
789

LVL 1
rahulsuryaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
You will need to use a BufferedReader to read the file this way:

http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html

Then you can split the string using the split () method.

String[] array = str.split ( "=" ) ;
array[1] will have "xyz" in the case of a=xyz and so on and so forth
0
rahulsuryaAuthor Commented:
how can i read the values between first write them again use same variable for next set
0
Mayank SAssociate Director - Product EngineeringCommented:
Come up with some code based on what I suggested and we'll help you further
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

rahulsuryaAuthor Commented:
String path = "c:"+ java.io.File.separator+"Documents and Settings"+ java.io.File.separator+"ranju"+ java.io.File.separator+"rahul";
 String f = path + java.io.File.separator+"test.txt";
 try {
        BufferedReader in = new BufferedReader(new FileReader(f));
        String str;
        while ((str = in.readLine()) != null) {
          process(str);
                  
        }
        in.close();
    } catch (IOException e) {
    }
}
public static void process(String str)
{
String[] array = str.split ( "=" ) ;
}


This is what i have written, but i have some problem displaying array values as it is dynamic.
0
Mayank SAssociate Director - Product EngineeringCommented:
In process (), you have to take the string array and process the elements depending on what you want to do.... do you want to display them or write them to an HTML file?
0
rahulsuryaAuthor Commented:
my original file has values like

url=http://...
x=..
y=...
text=....

i want to display text , pass url,x,y values to a servlet on clicking the button open url below each text.

So each jsp will display all the test corresponding to given one file and buttons to open url
0
Mayank SAssociate Director - Product EngineeringCommented:
I'm missing the point - where is the button? Is it on an applet or Swing JFrame and do you want to launch the browser? Or are you already viewing a JSP on the browser? If so where is the file - client side or server side. Pls don't assume that we know your set-up without your telling us.
0
rahulsuryaAuthor Commented:
My file is on server side and i want to display a jsp with texts and a submit button below each text with label " open url " .
Text will be taken from the file and url button will be redirected to servlet with values of url,x,y(again from txt file). This servlet takes this values and open the url in applet.
Hope my setup is clear :)

0
Ajay-SinghCommented:
Use LineNumberReader, something like this would do
 
LineNumberReader reader = new LineNumberReader(new FileReader("file"));
 
String line = reader.readLine();
while(line != null) {
    if(line.contains("=")) {
        int idx = line.indexOf("=");
        System.out.println(line.substring(line, idx+1);
    }
    line = reader.readLine();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rahulsuryaAuthor Commented:
how can i make this to read one set of file at a time and store values.
(my example file has 2 sets begining with <s> and ending with <e>)
0
Mayank SAssociate Director - Product EngineeringCommented:
With the file format being so random, you will probably have to check it yourself:

Hashtable separators = new Hashtable () ; // using a hashtable to speed up searching
separators.put ( "<s>", "<s>" ) ; // dummy value - only key is relevant
separators.put ( "<e>", "<e>" ) ;
ArrayList list = new ArrayList () ;

while ( ( str = in.readLine () ) != null )
{
  if ( separators.containsKey ( str ) )
    process ( list ) ;
 
  else
    list.add ( str ) ;

}
0
rahulsuryaAuthor Commented:
i didnt understand your last code mayankeagle sir.
0
Mayank SAssociate Director - Product EngineeringCommented:
Pls dont call sir.

Basically you will group strings into a list until the next separator like <e> or <s> comes, and then process the bunch.
0
rahulsuryaAuthor Commented:
Hi ajay your code is good but it reads say i have
text = xyz xyz xyz ...........
abc abc abc.........


it reads only first line of text as xyz xyz xyz ........... not the full para or sentence
0
rahulsuryaAuthor Commented:
ok but say if first element is seperator then it has empty list to process
0
rahulsuryaAuthor Commented:
and when i use split method my arraylist contains all values as
[Ljava.lang.String;@30c221, [Ljava.lang.String;@119298d, .........]
0
Mayank SAssociate Director - Product EngineeringCommented:
While processing, put the logic as:

if ( list.size () > 0 )
{
  // then process
}
0
Mayank SAssociate Director - Product EngineeringCommented:
Then don't use split (), just process them:

for ( int i = 0, count = list.size () ; i < count ; i ++ )
{
  String value = ( String ) list.get ( i ) ;
  // process 'value'
}

Or: String[] array = ( String[] ) list.toArray ( new String[0] ) ;

for ( int i = 0 ; i < array.length ; i ++ )
{
  // process array[i]
}
0
rahulsuryaAuthor Commented:
how is ArrayList passed as parameter?

I used

public static void process(ArrayList a2)
{
 ArrayList a3 = new ArrayList(a2);
.....
}

Is this correct
0
Mayank SAssociate Director - Product EngineeringCommented:
Just use: public void process ( ArrayList list )

or make it static depending upon what you wanna have.
0
rahulsuryaAuthor Commented:
i used this code to process

      for ( int i = 0, count = list.size () ; i < count ; i ++ )
            {
                    String value = ( String ) list.get ( i ) ;
                    if(value.contains("=")) {
            int idx = value.indexOf("=");
                    a1.add(value.substring(idx+1));}
 
 
            }

but when i use substring method for text value i get only first line not whole para
0
Mayank SAssociate Director - Product EngineeringCommented:
You want to completely ignore the <s> and <e> isn' it?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.