GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — java-basics/java-programs/MostRepeatedWord.java at master · learning-zone/java-basics Expand file tree/ Copy pathMostRepeatedWord.javaMore file actions 52 lines (46 loc) · 1.16 KB / Copy pathMostRepeatedWord.javaFile metadata and controls52 lines (46 loc) · 1.16 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package misc; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class MostRepeatedWord { public static void main(String[] args) { String line, word = ""; int count = 0, maxCount = 0; ArrayList<String> words = new ArrayList<String>(); // Opens file in read mode FileReader file; try { file = new FileReader("file.txt"); BufferedReader br = new BufferedReader(file); // Reads each line try { while ((line = br.readLine()) != null) { String string[] = line.toLowerCase().split("([,.\\s]+)"); for (String s : string) { words.add(s); } } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } // Determine the most repeated word in a file for (int i = 0; i < words.size(); i++) { count = 1; for (int j = i + 1; j < words.size(); j++) { if (words.get(i).equals(words.get(j))) { count++; } } if (count > maxCount) { maxCount = count; word = words.get(i); } } System.out.println("Most Repeated words: " + word); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |