Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Input file to array Posted by Azayth on 21 May 2012 at 10:37 AM
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!
Report
Here is the code... Posted by Manishankar on 22 May 2012 at 1:08 AM


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);
}
}
}


Report
Here is the code... Posted by Manishankar on 22 May 2012 at 1:11 AM


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);
}
}
}


Report
Here is the code... Posted by Manishankar on 22 May 2012 at 1:13 AM


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);
}
}
}


Report
Re: Here is the code... Posted by Azayth on 22 May 2012 at 3:48 AM
Thanks for that, but it seemed to just print the text file to the screen.
Report
I think its over check it now Posted by Manishankar on 22 May 2012 at 4:11 AM
/*
* 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
Report
Re: Input file to array Posted by Wirus on 23 May 2012 at 1:42 AM
Here you are.



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<String> sentences = new ArrayList<String>();

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


Cheers,
Wirus
Report
Re: Input file to array Posted by Wirus on 23 May 2012 at 1:45 AM
Here you are.



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<String> sentences = new ArrayList<String>();

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


Cheers,
Wirus



 

Recent Jobs