Results 1 to 14 of 14
- 09-28-2013, 09:22 PM #1
Member
- Join Date
- Sep 2013
- Posts
- 5
- Rep Power
- 0
Need some help to finish this code (finding the number of unique words ...)
How do I find the number of unique words and the total number of words from a random text file.
This is what I got so far...
------------------------------------------
import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.io.File;
class test {
public static void main(String [] args) {
File file = new File("...; "); // random text file
try {
String [] word;
int [] numer;
int numberOfUniqueWords;
int i;
String s;
String m= " ";
Scanner f = new Scanner(file);
while (f.hasNextLine()) {
String line = f.nextLine();
word=line.split(m);
for(i=0; i<word.length; i++) {
s=word[i];
System.out.println();
}
}
f.close();
}catch(Exception e) {}
}
}
- 09-29-2013, 05:07 AM #2
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 13
Re: Need some help to finish this code (finding the number of unique words ...)
Go through the file line by line, split by whitespace (similar to what you're doing, though for simplicity a space is fine). Then, I suggest using a HashMap<String, Integer> to keep track of each word's count. Basically, if the hashmap already contains the word, then you increment it's integer value. Otherwise, you add the value of 1 to it. Then, at the end, iterate through it.
- 09-29-2013, 10:41 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Need some help to finish this code (finding the number of unique words ...)
A small detail: don't split on a single space " " but split on "\\s+" instead.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 09-29-2013, 12:04 PM #4
Member
- Join Date
- Sep 2013
- Posts
- 5
- Rep Power
- 0
Re: Need some help to finish this code (finding the number of unique words ...)
Im trying to do the code without using HashMap, and Im stuck.
- 09-29-2013, 12:23 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-29-2013, 12:29 PM #6
Member
- Join Date
- Sep 2013
- Posts
- 5
- Rep Power
- 0
Re: Need some help to finish this code (finding the number of unique words ...)
So its not possible to do it without HashMap?
- 09-29-2013, 12:56 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Need some help to finish this code (finding the number of unique words ...)
Build a wall around Donald Trump; I'll pay for it.
- 09-29-2013, 01:07 PM #8
Member
- Join Date
- Sep 2013
- Posts
- 5
- Rep Power
- 0
Re: Need some help to finish this code (finding the number of unique words ...)
Whats Fortran ?
- 09-29-2013, 05:46 PM #9
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 13
- 09-29-2013, 05:51 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Need some help to finish this code (finding the number of unique words ...)
Build a wall around Donald Trump; I'll pay for it.
- 09-29-2013, 06:29 PM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Need some help to finish this code (finding the number of unique words ...)
Ahh! My first programming language. Fortran IV. Computed gotos; arithmetic if statements. And it actually passed values by name. You could change the value of literal constants!
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-29-2013, 06:43 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-29-2013, 06:47 PM #13
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Need some help to finish this code (finding the number of unique words ...)
Actually, I liked equivalence relations. I wrote a solitaire game. All the variables had a specific location in the huge equivalence array. When I wanted to save state so that someone could resume their game at a later time. I just wrote out the array. It could be read it later to resume the game. Setting them all up and debugging was somewhat of a pain.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-29-2013, 07:07 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Need some help to finish this code (finding the number of unique words ...)
Those were the 'lame' uses of equvalence relations; the real 'fun' (mind the quotes) starts when you define an equivalence relation between D and IA where D is a REAL*8 and IA is followed by IB, as in INTEGER*4 IA, IB; oh, the sheer fun we had ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
Finding number of lines, words, and characters in a file
By csrLewis93 in forum New To JavaReplies: 1Last Post: 02-25-2013, 05:41 AM -
Help with finding how many words there are in a sentence.
By Happypanda in forum New To JavaReplies: 3Last Post: 07-09-2012, 12:08 AM -
Arbitrary number of objects, each with a unique name.
By providence in forum New To JavaReplies: 8Last Post: 02-23-2011, 02:12 AM -
Unique personal number
By Lidiya in forum Advanced JavaReplies: 9Last Post: 12-27-2010, 01:13 PM -
Finding words (and more) in another file
By Lund01 in forum Java AppletsReplies: 5Last Post: 09-30-2010, 02:03 PM
Bookmarks