Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

load all files in a directory

807601Apr 19 2008 — edited Apr 19 2008
hi all! im new to java and i was wondering if u can help me out!
ive wrote a small application which analysises words in a text file, and what i need to do is load all the files in a directory, rather than one at a time like in the following code. I understand that a loop is needed but i really dont know where to start! please help!
thanks for your time
oh also, is there a way of the system printing the filename rather than "this text is.."?
import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;




public class eval {


private String[] words;
private int[] values;
private int num;
private HashMap <String, Integer> hashMap;

private arrays a;
private String[] negwords;
private int[] negvalues;
private HashMap hashMap2;
private int negnum;

public eval() {
initiatemap();
BufferedReader in = null;
try {
in = new BufferedReader (new FileReader ("text.txt")); //id like this to load all files in a directory

String str;
String s;
while ((str = in.readLine()) != null)
{

str = str.replaceAll("[\\p{Punct}&&[^?????????????????????]]", " ");
String[]temp = str.split(" ");




for (int i = 0; i < temp.length; i++) {
if (hashMap.containsKey(temp)) {

num++;
}}
for (int i = 0; i < temp.length; i++) {
if (hashMap2.containsKey(temp[i])) {

negnum++;

}
}

}

if (num > 0 && negnum > 0 ){

double total = num + negnum;

double txt1 = round((num/total)*100, 2);


if (txt1 < 50)

System.out.println("This text is " + txt1 + "% positive and is therefore not a happy text.");
else if (txt1 >= 50 && txt1 <=65)
System.out.println("This text is " + txt1 + "% positive, but does contain " + (100 - txt1) + "% negative words, it is therefore probably not a happy text.");
else if (txt1 > 65)
System.out.println("This text is " + txt1 + "% positive, and is therefore a happy text.");
in.close();
}}

catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static double round(double val, int places) {
long factor = (long)Math.pow(10,places);

// Shift the decimal the correct number of places
// to the right.
val = val * factor;

// Round to the nearest integer.
long tmp = Math.round(val);

// Shift the decimal the correct number of places
// back to the left.
return (double)tmp / factor;
}

public void initiatemap()
{
a = new arrays();
words = a.initiateWords();
negwords = a.initiateNegWords();
values = a.initiateValues();
negvalues = a.initiateNegValues();
hashMap = a.initiateMap();
hashMap2 = a.initiateNegMap();
for (int i = 0; i < words.length; i++) {
hashMap.put(words[i], values[i]);}

for (int j = 0; j < negwords.length; j++) {
hashMap2.put(negwords[j], negvalues[j]);}
}

public static void main(String[] args) {
eval eval = new eval();
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 17 2008
Added on Apr 19 2008
10 comments
185 views