java - How to replace the first word from each sentence (from input file) -


my problem have input file , must rewrite text, in output file without 4 words("a"),("the"),("a"),("the").i managed solve "a" , "the", not "a" , "the". plz me code? in advance. below problem,the input , code:

problem:

the english, words "a" , "the" can removed sentences without affecting meaning. opportunity compressing size of text files! write program inputs text file, line-by-line, , writes out new text file each line has useless words eliminated.

first write simple version of program replaces substrings " " , " " in each line single space. remove many words, these words occur @ beginnings or ends of lines, , words start capitals. so, improve first program handles situations well.

c:>java remover < verbose.txt > terse.txt

note: there various replace() methods of class string simplify program. try write program without using them.

input file:

a novel long prose narrative describes fictional characters , events, in form of sequential story. genre has historical roots in fields of medieval , modern romance , in tradition of novella.

code:

import java.util.scanner; import java.io.*;  class file_compressor {  public static void main(string[]args) throws ioexception   {     int loc=0;   string line="";     file input=new file ("input.txt");    scanner scan=new scanner(input);    file output=new file("hello2.java");    printstream print=new printstream(output);     while (scan.hasnext())        {line=scan.nextline().trim();              while(line.indexof("a")>0||line.indexof("the")>0||line.indexof(" a")>0||line.indexof(" ")>0)    {    if (line.indexof("a")>0)      {loc=line.indexof("a");      line=line.substring(loc+1);}       else if (line.indexof("the")>0)      {loc=line.indexof("the");      line=line.substring(loc+3);          }       else if (line.indexof(" ")>0)      {loc=line.indexof(" ");      left=line.substring(0,loc+1);      right=line.substring(loc+2);      line=left+right;}       else if (line.indexof(" ")>0)      {loc=line.indexof(" ");      left=line.substring(0,loc+1);      right=line.substring(loc+4);      line=left+right;}      }      print.println(line);      }  } 

}

a slight modification of code might trick already. didn't have chance read thoroughly, try (expand the etc.):

if (line.startswith("a ")) {  loc=line.indexof("a ");  line=line.substring(loc+2); } 

there few assumptions, though:

  • each line contains single sentence
  • there's spaces used whitespace (no tabs)

as side note: inner while condition should match tests inside, i.e. should " " instead of " a".

another option use regular expressions via pattern , matcher classes, i.e. implement logic of string.replaceall(...) - if allowed.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -