0

i have wrote the code to read a 3rd column of a file(treeout1.txt) and write those contents in another file(tree.txt).and now i want to open tree.txt and write the contents to stem.txt,where tree.txt contents a single word in each row and a delimiter is found at the end of each line.i have attached that txt file below.you can view that to have better understanding.now i want to write the words into a line till a delimiter "###" is found...for example 'the girl own paper' and next line vol and so on....i have tried that but ArrayIndexOutOfBoundsException comes for a[]...why?and how to resolve that?

the text file tree.txt is given below

the
girl
own
paper
###
vol
###
viii
###
no
###
@card@
###
October
@card@
@card@
###
price
one
penny
###
as
the
baron
have
conjecture
the
housemaid
whom
he
have
call
out
of
the
nursery
to
look
for
###
lons
cane
on
find
her
master
have
go
without
it
do
not
hurry
back
but
stop
talk
###

Code:

 package simple;

 import java.io.*;
 import java.util.Scanner;
 import java.util.StringTokenizer;

 public class Simple {

   public static void main(String[] args) throws IOException {
     String line;
     String line2;
     String[] a = new String[100];
     int i = 0;
     try {
       BufferedReader br = new BufferedReader(new FileReader("C:/TreeTagger/treeout1.txt"));
       BufferedWriter output = new BufferedWriter(new FileWriter("D:/tree.txt"));
       String separator = System.getProperty("line.separator");
       while ((line = br.readLine()) != null) {
         StringTokenizer st2 = new StringTokenizer(line, "\n");
         while (st2.hasMoreElements()) {
           String line1 = (String) st2.nextElement();
           String[] array = line1.split("\\s+", 3);
           //System.out.println(array[2]);
           output.append(array[2]);
           output.newLine();
         }
       }
       output.close();
       br.close();
       BufferedReader br1 = new BufferedReader(new FileReader("D:/tree.txt"));
       BufferedWriter out = new BufferedWriter(new FileWriter("D:/stem.txt"));
       while ((line2 = br1.readLine()) != null) {
         StringTokenizer st = new StringTokenizer(line2, " ");
         while (st.hasMoreTokens()) {
           String element = st.nextToken();
           System.out.println(element);
           while (element != "###") {

             a[i] = element;
             i++;
           }
           out.append(a[i]);
           element = element.replace(element, "");

         }

       }
     } catch (IOException e) {

     }
   }
 }
Samuel O'Malley
  • 3,471
  • 1
  • 23
  • 41
saranya
  • 85
  • 8

1 Answers1

0

You need to reset i to 0 after you find the ### delimiter. Otherwise you will keep incrementing i until it gets larger than 100 (a's maximum).

Also you can't use the != operator on Strings (from your code: element != "###"). You need to use the following:

!"###".equals(element);
Samuel O'Malley
  • 3,471
  • 1
  • 23
  • 41
  • s...i have tried with reseting i...no improvement..same error is coming – saranya Nov 04 '13 at 04:17
  • Have a look at my edit, should be `!"###".equals(element);` instead of `element != "###"` – Samuel O'Malley Nov 04 '13 at 04:20
  • thank u...even now i get error...this is my error run: the Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at simple.Simple.main(Simple.java:55) Java Result: 1 BUILD SUCCESSFUL (total time: 1 second) only 1st word is read 'the' – saranya Nov 04 '13 at 04:23
  • Your `while (element != "###") {` should just be an if statement I think. Your logic on the while loops looks wrong to me. That while look for example will never finish because element will never be "###" within the loop. – Samuel O'Malley Nov 04 '13 at 04:26
  • s...wat u have told is right...if i work with if condition it prints correctly....but now i am trying to put the contents in array...thanks for ur help – saranya Nov 04 '13 at 04:32
  • My pleasure, if your problem has been solved then feel free to mark the Answer as accepted. Thanks and good luck – Samuel O'Malley Nov 04 '13 at 04:36