Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package words_occurences;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.LinkedList;
- import java.util.Scanner;
- /*class wordInfo {
- String word;
- int occurences;
- public wordInfo(String word, int occurences) {
- this.word = word;
- this.occurences = occurences;
- }
- }
- */
- public class MainLinkedList {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- File f = new File("text.txt");
- LinkedList<wordInfo> words = new LinkedList<wordInfo>();
- try {
- Scanner sc = new Scanner(f);
- // sc.useDelimiter("\\s\\p{Punct}");
- while (sc.hasNext()) {
- String str = sc.next();
- boolean found = false;
- for (wordInfo i : words) {
- if (i.word.equals(str)) {
- i.occurences++;
- found = true;
- System.out.println("Found! word=" + str + ", occurences=" + i.occurences);
- }
- }
- if (!found)
- words.add(new wordInfo(str, 1));
- }
- System.out.println("Word: Occurences:");
- for (wordInfo i : words) {
- System.out.println(i.word + " " + i.occurences);
- }
- sc.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement