9 Replies - 285 Views - Last Post: 01 December 2012 - 11:27 AM Rate Topic: -----

#1 RobertEnglish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 07-November 11

give task to make simple word analyser from a text file

Posted 30 November 2012 - 11:01 AM

// So far I've only made the File reader and IO exception handler.
// really stumped where to go from here tbh.
//

package Project;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Robert
 */
public class simpleWord {

    private ArrayList<String> fileInfo = new ArrayList<String>();

    public void simWordReader() {

        try {
            String filePath = JOptionPane.showInputDialog("Please enter Filename");

            FileReader file = new FileReader(filePath);
            BufferedReader reader = new BufferedReader(file);

            while (reader.ready()) {
                fileInfo.add(reader.readLine());
            }

        } catch (IOException io) {
            System.out.println(io);
        }
    }
}


This post has been edited by macosxnerd101: 30 November 2012 - 12:58 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: give task to make simple word analyser from a text file

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: give task to make simple word analyser from a text file

Posted 30 November 2012 - 11:18 AM

What excatly do you want to do?
Was This Post Helpful? 0
  • +
  • -

#3 RobertEnglish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 07-November 11

Re: give task to make simple word analyser from a text file

Posted 30 November 2012 - 12:14 PM

I need to analyse a text file in a directory. the analysis needs to count the number of words, count the number of different words(not case sensitive), give a list of words in order if appearance and occurrences of each word in order of appearance.
this is one of four analysis types i've been asked to do and I am absolutely lost.
I have a basic algorithm for the first part of this one but I don't know enough to implement it.
Was This Post Helpful? 0
  • +
  • -

#4 RobertEnglish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 07-November 11

Re: give task to make simple word analyser from a text file

Posted 30 November 2012 - 12:29 PM

I know this goes against what this site is for but this is a last attempt at finding help
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,256
  • Joined: 27-December 08

Re: give task to make simple word analyser from a text file

Posted 30 November 2012 - 01:05 PM

  • Counting the number of words: Think about using a loop and a BufferedReader or Scanner here. Each time you read in a word, increment a counter. If you read in line by line, split() (see the String split() method) the line on whitespace and add the size of the array to the count variable.

  • Take a look at the HashMap class as well, and place Strings in a HashMap as the keys, with the counts as the values. So HashMap<String, Integer>. If the String already exists, put() it with an updated count. Otherwise, put() it in the Map with a count of 1. This makes your remaining analyses easier to implement.


We won't give you the code, but we are happy to help if you run into problems or errors when implementing a solution.
Was This Post Helpful? 1
  • +
  • -

#6 RobertEnglish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 07-November 11

Re: give task to make simple word analyser from a text file

Posted 30 November 2012 - 01:22 PM

Thanks for that. I was only hoping for something like an algorithm. In this project we're asked to use a number of different analysis methods. Hashing is one of them but I haven't even started that yet either.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,256
  • Joined: 27-December 08

Re: give task to make simple word analyser from a text file

Posted 30 November 2012 - 01:25 PM

Quote

I was only hoping for something like an algorithm.

An algorithm is just a finite sequence of steps that produces a specific result. The counting the number of words step is an algorithm. The rest of it should give you enough to start thinking more about it yourself.

And the HashMap handles the hashing internally, so you don't really have to get into the nitty-gritty. :)
Was This Post Helpful? 0
  • +
  • -

#8 RobertEnglish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 07-November 11

Re: give task to make simple word analyser from a text file

Posted 01 December 2012 - 05:26 AM

I've come up with a basic psuedo code for this but i'm unsure if it is going to do everything as planned. i think i've been looking at this for too long and i'm just not registering simple mistakes.
here it is. any help would be greatly appreciated!

fileInfo is the text being read from the reader
int currentWord = 0;
int wordCound = 0;
for (int i=0; i<fileInfo; i++){

    if((nextChar==spaceChar)||(nextChar==numberChar)||(nextChar==symbolChar)){
      wordCount++;
    }else if(nextChar==letterChar){
    currentWord += element@ position[i]
   }
}


This post has been edited by macosxnerd101: 01 December 2012 - 10:01 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,256
  • Joined: 27-December 08

Re: give task to make simple word analyser from a text file

Posted 01 December 2012 - 10:05 AM

Again- check out the String split() method. It will make your life a lot easier:
BfferedReader reader = new BufferedReader(new FileReader("filename.txt"));

int count = 0;
String line = "";

while((line = reader.readLine()) != null){
   String[] tokens = line.split(" ");
   count += tokens.length;
}


Was This Post Helpful? 0
  • +
  • -

#10 RobertEnglish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 07-November 11

Re: give task to make simple word analyser from a text file

Posted 01 December 2012 - 11:27 AM

This is great. I had Googled the String.split method and only found programs I didn't understand.
we hadn't yet covered anything about tokens or the split method. I'll do some research to get a better understanding. Thanks agian
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1