Cael February 2016

Java reading and splitting sentences from a text file

I have a code that will read from a text file and store each sentences to an array. This is the 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]);

        for (int r=0;r<sentenceArray.length;r++)
        {
            SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*"); //split sentences and store in array 
        }

        for (int i=0;i<SENTENCE.length;i++)
        {
            System.out.println("Sentence " + (i+1) + ": " + SENTENCE[i]);
        }

     }
}

This is the content in the input7.txt

Shocking images of a Taiwan apartment complex felled like a tree by an earthquake have highlighted what is needed to build a structure that can withstand seismic shocks.
Like Taiwan, Japan is quake-prone -- it suffers about a fifth of the world’s most powerful tremors. It has used a mix of ancient and modern technologies to make its buildings increasingly quake-proof.
Lessons have been consistently learnt and building standards subsequently raised in the wake of deadly disasters such as the 1995 Kobe earthquake, which killed 6,434 people.
When a massive magnitude earthquake struck off northeastern Japan on March 11, 2011, the shaking in Tokyo was violent. But buildings -- including the nearly complete 634-metre (2,080 feet) Tokyo Skytree tower and other skyscrapers -- survived intact.

However, the code will only read in and display the sentences in the last line of the file:


      

Answers


Vihar February 2016

the line that causes all problem is

SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*");

during each iteration you are replacing the previous value of SENTENCE with a new value so this problem arises,

clubbing the for loop will definitely solve the problem, but if you want to use the contents of SENTENCE array after this exercise then it wont help.

hope this helps!

Good luck!


Berger February 2016

You must put your second loop inside the first one, or it will only print the result for the last value of SENTENCE :

 for (int r=0;r<sentenceArray.length;r++)
  {
     SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*"); //split sentences and store in array 

     for (int j=0;j<SENTENCE.length;j++)
     {
        System.out.println("Sentence " + (j+1) + ": " + SENTENCE[j]);
     }

  }


Vpm February 2016

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()]);

       for (int r=0;r<sentenceArray.length;r++)
       {
           SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*");
           for (int i=0;i<SENTENCE.length;i++)
           {
               System.out.println("Sentence " + (i+1) + ": " + SENTENCE[i]);
           }

       }

   }

Adding the Second for loop inside the first should Help :) !

Post Status

Asked in February 2016
Viewed 3,199 times
Voted 5
Answered 3 times

Search




Leave an answer