Anatch
Anatch

Reputation: 453

How to Get a value from a text file

My problem : I need to get several values from a text file.

I am working on a parser from the ttcn-3 language to the XML language and what I need to do is to get some key words and values from the ttcn module which is written in an text file to build the XML file.

Here is the start of my ttcn code:

module SessionSetup
{
type port InputPortType message { in charstring }
type port OutputPortType message { out charstring }
type component SessionSetupResComponentType {
    port InputPortType InputPort;
    port OutputPortType OutputPort;
}

For example I need to get the string which is right after module (SessionSetup) and here is the code I made:

public  void ReadTheFileGetTheModuleName(String fichier){
            try{
                InputStream ips=new FileInputStream(fichier); 
                InputStreamReader ipsr=new InputStreamReader(ips);
                BufferedReader br=new BufferedReader(ipsr);
                String ligne;

                while ((ligne=br.readLine())!=null){
                    if (ligne.contains("module"))
                    {
                        String[] st = ligne.split(ligne, ' ');
                        System.out.println("Nom = "+st[1]);
                    }

                    chaine+=ligne+"\n";

                }
                br.close();
            }       
            catch (Exception e){
                System.out.println(e.toString());
            }
        }

The problem is that it doesn't work, I don't get the string after module.

Thanks

Upvotes: 1

Views: 104

Answers (1)

Neeraj Jain
Neeraj Jain

Reputation: 7730

 String[] st = ligne.split(ligne, ' ');
                       /\/\/\/\/\/\/\
                 Incorrect way, pass some regex on which string should split

Your String#Split() method is wrong , Please check the documentation

it should be like this

public String[] split(String regex, int limit)

or

public String[] split(String regex)

Note : You will not get a compile time error , Since this (' ') in split() method is a character and can be implicitly type casted to Integer.

Upvotes: 1

Related Questions