Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Input file to array

AzaythAzayth Posts: 2Member
Hi everyone!

I'm new to Java and I was wondering if anyone could help me with a program I'm working on. I have to read in a text file (I know how to do that part) and count the number of sentences in it, then save all the sentences to an array so I can pull out, say, sentence 70 on demand.

Any help will be greatly appreciated, thanks!

Comments

  • ManishankarManishankar Posts: 5Member


    import java.io.*;
    public class FiletoArray {
    public static void main(String args[])
    {
    try
    {
    FileInputStream f=new FileInputStream("C:/Users/user/Desktop/aa.txt");
    String s[]=new String[500]; //To store data from file
    String temp; //Used to retrive current line data
    int i=0;
    System.out.println("ENTER DATA");
    DataInputStream di=new DataInputStream(f); //Used to retrive any types of data
    BufferedReader br=new BufferedReader(new InputStreamReader(di));

    System.out.println(br.readLine());
    while((temp=br.readLine())!=null)
    {

    s[i]=temp;
    System.out.println(s[i]);
    i++;
    }
    di.close();
    System.out.println("NUMBER OF LINES IN THE FILE: "+(i+1));

    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }

    [color=Green][/color]
  • ManishankarManishankar Posts: 5Member


    import java.io.*;
    public class FiletoArray {
    public static void main(String args[])
    {
    try
    {
    FileInputStream f=new FileInputStream("C:/Users/user/Desktop/aa.txt");
    String s[]=new String[500]; //To store data from file
    String temp; //Used to retrive current line data
    int i=0;
    System.out.println("ENTER DATA");
    DataInputStream di=new DataInputStream(f); //Used to retrive any types of data
    BufferedReader br=new BufferedReader(new InputStreamReader(di));

    System.out.println(br.readLine());
    while((temp=br.readLine())!=null)
    {

    s[i]=temp;
    System.out.println(s[i]);
    i++;
    }
    di.close();
    System.out.println("NUMBER OF LINES IN THE FILE: "+(i+1));

    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }

    [color=Green][/color]
  • ManishankarManishankar Posts: 5Member


    import java.io.*;
    public class FiletoArray {
    public static void main(String args[])
    {
    try
    {
    FileInputStream f=new FileInputStream("C:/Users/user/Desktop/aa.txt");
    String s[]=new String[500]; //To store data from file
    String temp; //Used to retrive current line data
    int i=0;
    System.out.println("ENTER DATA");
    DataInputStream di=new DataInputStream(f); //Used to retrive any types of data
    BufferedReader br=new BufferedReader(new InputStreamReader(di));

    System.out.println(br.readLine());
    while((temp=br.readLine())!=null)
    {

    s[i]=temp;
    System.out.println(s[i]);
    i++;
    }
    di.close();
    System.out.println("NUMBER OF LINES IN THE FILE: "+(i+1));

    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }

    [color=Green][/color]
  • AzaythAzayth Posts: 2Member
    Thanks for that, but it seemed to just print the text file to the screen.
  • ManishankarManishankar Posts: 5Member
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author user
    */

    import java.awt.event.KeyEvent;
    import java.io.*;
    public class FiletoArray {
    public static void main(String args[])
    {
    try
    {
    FileInputStream f=new FileInputStream("C:/Users/user/Desktop/aa.txt");
    String s[]=new String[500];
    String temp;
    int i=0,x=0;
    System.out.println("ENTER DATA");
    DataInputStream di=new DataInputStream(f);
    BufferedReader br=new BufferedReader(new InputStreamReader(di));

    System.out.println(br.readLine());
    while((temp=br.readLine())!=null)
    {

    s[i]=temp;

    i++;
    }
    di.close();
    /*for(int j=0;j<s.length;j++)
    {
    System.out.println(s[j]);
    }
    */
    System.out.println("DATA RETRIVED FROM ARRAY: "); //Displaying data stored in array
    while(s[x]!=null)
    {
    System.out.println(s[x]);
    x++;
    }
    System.out.println("NUMBER OF LINES IN THE FILE: "+(i+1));//Number of Lines present in File

    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }


    Stay Hungry Stay Foolish
  • WirusWirus Posts: 10Member
    Here you are.


    [code]

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.regex.Pattern;


    public class SentenceCounter {
    private static final Pattern pattern = Pattern.compile( "\." );

    public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner( new File( "E:\file.txt") ).useDelimiter( pattern );
    List sentences = new ArrayList();

    while( scanner.hasNext() ){
    String sentence = scanner.next().trim().replaceAll("[
    ]+", " ") + ".";
    sentences.add( sentence );
    // System.out.println( sentence );
    }
    }
    }
    [/code]

    Cheers,
    Wirus
  • WirusWirus Posts: 10Member
    Here you are.


    [code]

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;


    public class SentenceCounter {

    public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner( new File( "E:\file.txt") ).useDelimiter( "\." );
    List sentences = new ArrayList();

    while( scanner.hasNext() ){
    String sentence = scanner.next().trim().replaceAll("[
    ]+", " ") + ".";
    sentences.add( sentence );
    System.out.println( sentence );
    }
    }
    }
    [/code]

    Cheers,
    Wirus
Sign In or Register to comment.