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
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]
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]
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]
* 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
[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
[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