Hello Everybody!
I would like to tokenize the text file and save the token into the array.
I will tokenize a sentence if it starts with “[(“ and consider the sentence ends when it meets “)]”
For example, if a text file contains

[(I love
Rose)]
Please help me
[(Thanks in advance)]

I love Rose is a sentence and I would like to save the tokens I, love, Rose into S1[0][0],S1[0][1],S2[0][2] repectively.

Similarly, I would like to save the tokens Thanks, in , advance into S1[1][0],S1[1][1],S1[1][2] respectively.

Here is my coding.
I see null value in the array.
What is the problem?

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class start4
{
public static File f;
public static FileInputStream fis;
public static String s;
public static BufferedReader d;
public static void main(String args[])
{
int senno=0;
int i=0;
String S1[][];
S1=new String[1000][1000];
try
{
d=null;
//f=new File("C:/myfile.txt");
f = new File("C:/link4/data/test1OUT.txt");
fis = new FileInputStream(f);
d = new BufferedReader(new InputStreamReader(fis));
while((s=d.readLine())!= null)
{
if(s.startsWith("[("))
{
senno++;
StringTokenizer st = new StringTokenizer(s," []()");
while (st.hasMoreTokens( ))
{
S1[senno][i++]=st.nextToken(); }
//System.out.println(st.nextToken());} //print all the token of a line
if(!(s.endsWith(")]")))
{
while(!(s.endsWith(")]")))
{
s=d.readLine();
st = new StringTokenizer(s," []()");
while (st.hasMoreTokens( ))
{S1[senno][i++]=st.nextToken();}
//{ System.out.println(st.nextToken());}
}
}
}
}
for(int j=1;j<=senno;j++)
for(int k=0;k<i;k++)
System.out.println(S1[j][k]);
}//try
catch(FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
finally {
if (d!= null) {
try {
d.close(); // may throw an exception
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
 
 
 
 
}//finally
}//main
}//class

String.split() works better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.