right, so ive got this problem, basically, ive got a bunch of text files and i want to use the scanner to go through every single file, and seperate every single word from all the text files, i tried everythnig, and ive scoured this forum my code looks like this:

import java.io.*;
import java.util.Scanner;

public class test {
    public static void main(String[] args) throws IOException {

        Scanner scanner = null;
        File dir = new File("C:/Documents and Settings/User/My Documents/corpus.txt"); // here i want to load not one but many files

        try {
            scanner = new Scanner(new BufferedReader(new FileReader(dir)));

            while (scanner.hasNext()) {

                System.out.println(scanner.next());
            }
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    }
}

as you can see above there is the line where u load the single text file, but i wanna load in like 5 text files, help anyone?

Recommended Answers

All 11 Replies

You just need to open a scanner on each file in your file list and process it, i.e.

String[] fileList = {"file1.txt","file2.txt","file3.txt"};
for (String filename : fileList){
    Scanner scanner = new Scanner(new File(filename));
    while (scanner.hasNext()){
        // blah blah
    }
    scanner.close();
}

Edit: Used wrong constructor on the Scanner.

its givin me an error saying i can only add one file in the parameter :S, cant add all them files dude :(

basically, this code, is seperate out all the words from text file and then save each of them into a hashmap (which i havent started yet), any takers?

In what parameter? Are we supposed to look over your shoulder at what you typed? You create a new Scanner instance for each file in a list (or any other collection). If you're getting errors trying to do that, you need to post your code and the exact error message.

By the way, my code fragment above used the wrong constructor form (it passed the string file name instead of a File object) and has been edited accordingly.

basically, this code, is seperate out all the words from text file and then save each of them into a hashmap (which i havent started yet), any takers?

"Any takers" on finishing the work for you? No. Helping you complete it is still a possibility.

fair douz lol, where did you change the code, was it String [] filelist that was wrong?, btw that was my mistake what you wrote was fine.

In what parameter? Are we supposed to look over your shoulder at what you typed? You create a new Scanner instance for each file in a list (or any other collection). If you're getting errors trying to do that, you need to post your code and the exact error message.

By the way, my code fragment above used the wrong constructor form (it passed the string file name instead of a File object) and has been edited accordingly.

Like Ezzaral said post your code and where you are getting the error. Also remove the throws Exception from the main. What would be the point of the method throwing an exception since no one will catch it. Also you need to add the catch (IOException ioe) { ... } after the try { .. }

Like Ezzaral said post your code and where you are getting the error. Also remove the throws Exception from the main. What would be the point of the method throwing an exception since no one will catch it. Also you need to add the catch (IOException ioe) { ... } after the try { .. }

import java.io.*;
import java.util.*;

public class concordance {


    public static void main(String[] args) throws IOException {

        String[] fileList = {   "C:/Users/workspace/emma_chapter1.txt",
                                "C:/Users/workspace/emma_chapter2.txt",
                                "C:/Users/workspace/a_christmas_carol_chapter1.txt",
                                "C:/Users/workspace/a_christmas_carol_chapter4.txt",
                                "C:/Users/workspace/pride_and_prejudice_chapter3.txt",
                                "C:/Users/workspace/pride_and_prejudice_chapter42.txt",
                                "C:/Users/workspace/spirits_in_bondage.txt",
                            };


        for (String filename : fileList){
        Scanner scanner = new Scanner(new File(filename));
         while (scanner.hasNext()){


              System.out.println(scanner.next());
            }

        }


    }
}

so far i have this, it loads in all the files that i wanted, all the words are seperated out and listed down the side, but i need to add all the parsed words to a hashmap, n i have no idea how to

First of all I think you need to remove the 'comma'

String[] fileList = { "C:/Users/workspace/emma_chapter1.txt",
"C:/Users/workspace/emma_chapter2.txt",
"C:/Users/workspace/a_christmas_carol_chapter1.txt",
"C:/Users/workspace/a_christmas_carol_chapter4.txt",
"C:/Users/workspace/pride_and_prejudice_chapter3.txt",
"C:/Users/workspace/pride_and_prejudice_chapter42.txt",
"C:/Users/workspace/spirits_in_bondage.txt",
};

Then if how to use a hashmap is your problem then why don't you try looking at the java APIs for the class HashMap. In there you can find all the methods and their explanations

ill check it out most definatley, if i have any major problems ill start a new thread thanks everyone for helpin a sucker out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.