Java reading and splitting sentences from a text file -


i have code read text file , store each sentences array. code:

import java.io.file; import java.util.arraylist; import java.util.scanner;  public class helloworld{      static string[] sentence;        public static void main(string []args) throws exception{          scanner sentence = new scanner(new file("assets/input7.txt"));         arraylist<string> sentencelist = new arraylist<string>();          while (sentence.hasnextline())         {             sentencelist.add(sentence.nextline());         }          sentence.close();          string[] sentencearray = sentencelist.toarray(new string[0]);          (int r=0;r<sentencearray.length;r++)         {             sentence = sentencearray[r].split("(?<=[.!?])\\s*"); //split sentences , store in array          }          (int i=0;i<sentence.length;i++)         {             system.out.println("sentence " + (i+1) + ": " + sentence[i]);         }       } } 

this content in input7.txt

shocking images of taiwan apartment complex felled tree earthquake have highlighted needed build structure can withstand seismic shocks. taiwan, japan quake-prone -- suffers fifth of world’s powerful tremors. has used mix of ancient , modern technologies make buildings increasingly quake-proof. lessons have been consistently learnt , building standards subsequently raised in wake of deadly disasters such 1995 kobe earthquake, killed 6,434 people. when massive magnitude earthquake struck off northeastern japan on march 11, 2011, shaking in tokyo violent. buildings -- including complete 634-metre (2,080 feet) tokyo skytree tower , other skyscrapers -- survived intact. 

however, code read in , display sentences in last line of file:

sentence 1: when massive magnitude earthquake struck off northeastern japan on march 11, 2011, shaking in tokyo violent. sentence 2: buildings -- including complete 634-metre (2,080 feet) tokyo skytree tower , other skyscrapers -- survived intact. 

anyone has idea how can make program display sentences in file beginning of line till last? thanks!

one approach is:

static string[] sentence;      public static void main(string []args) throws exception{         scanner sentence = new scanner(new file("assets/input7.txt"));        arraylist<string> sentencelist = new arraylist<string>();         while (sentence.hasnextline())        {            sentencelist.add(sentence.nextline());        }         sentence.close();         string[] sentencearray = sentencelist.toarray(new string[sentencelist.size()]);         (int r=0;r<sentencearray.length;r++)        {            sentence = sentencearray[r].split("(?<=[.!?])\\s*");            (int i=0;i<sentence.length;i++)            {                system.out.println("sentence " + (i+1) + ": " + sentence[i]);            }         }     } 

adding second loop inside first should :) !


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -