Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Parsing of input file - splitting by \n

807591Jun 19 2008 — edited Jun 20 2008
Hi All,

I am facing a pretty weird problem, I kinda gave up so I would love it if someone could help me out.
I am writing an application which part of what its doing it reads a file and parses the input to do something on it. The thing is the file includes a string "\n" which I would like to split by it... here's a sample:

graph 1.000 8.597 11.000
node 27 1.375 10.597 1.216 0.786 "START\nAC=2661" solid ellipse blue lightgrey
node 29 3.667 0.958 1.216 0.786 "END\nAC=2661" solid ellipse blue lightgrey
edge 27 69 4 1.806 10.306 2.319 9.958 3.194 9.375 3.750 9.000 "PRB=0.999\nDUR=8/30474/50428\nAC=2658" 4.083 9.611 solid blue

my program goes over the "node" lines and tries to do something with them, but when trying to split the "START\nAC=2661" for example using split("\n") the split does not work. its as if it does not see the \n as \n, I tried using split("\\n") and it did not help either.
Here's my code (reduced just to show the problem)
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import java.util.*;

class Test3
{
  public Test3()
  {
  }

  static private boolean CreateAndParseDotFile( String dottyFileName )
  {
    BufferedReader in;
    try 
    {
      in = new BufferedReader(new FileReader( dottyFileName ));
    } 
    catch ( Exception e )
    {
      System.out.println( " Could not open file" + dottyFileName + "!!!");
      return false;
    }
  
  	//start reading input
  	String input;
  	    
  	try 
  	{
  		input= in.readLine();
  	} 
  	catch ( Exception e )
  	{
        System.out.print("Fail 0");
  	    return false;
  	}
  	  
  	int numTokens;
  	String token;
    String m_label;
  	
	  while ( input != null )
	  {
	    StringTokenizer tokens = new StringTokenizer(input, " "); 
	    numTokens	= tokens.countTokens(); 
	    if ( numTokens >= 7 )
	    {
	  	 token = tokens.nextToken().trim();
	  	 if ( token.equals("node") )
	  	 {
	  		 token = tokens.nextToken().trim(); //node Id - skip
	  		 token = tokens.nextToken().trim();
	  		 // initialize new node entry
	  		 
	  		 token = tokens.nextToken().trim();
	  		 //newNode.m_y = ( new Double( token )).doubleValue();
	  		 
	  		 token = tokens.nextToken().trim();
	  		 //newNode.m_width = ( new Double( token )).doubleValue();
	  		 
	  		 token = tokens.nextToken().trim();
	  		 //newNode.m_height = ( new Double( token )).doubleValue();
	  		 
	  		 token = tokens.nextToken().trim();
	  		 System.out.println("token before removing the \"  ==> " + token);
	  		 m_label = token.replaceAll("\"","");

	       System.out.println("token after removing the \" =>  " + m_label);
	  
	       String[] labelParts = m_label.split("\n");
         
         System.out.println("Split of the token:");
         for (int i=0; i< labelParts.length;i++)
            System.out.println("" + i + " = " + labelParts);
//do something with it

}
}

try
{
input= in.readLine();
}
catch ( Exception e)
{
System.out.print("Fail 1");
return false;
}
}
return true;
}
public static void main(String[] args)
{
CreateAndParseDotFile( args[0] );
}
}
{code}

Please try to help

Thanks in advance
Eman
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 18 2008
Added on Jun 19 2008
7 comments
213 views