Created
August 6, 2013 15:46
-
-
Save anonymous/6165726 to your computer and use it in GitHub Desktop.
Challange 125
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import javax.swing.JOptionPane; | |
import java.io.File; | |
import java.util.Arrays; | |
import java.util.HashSet; | |
public class Challange125 { | |
public static int findNumberOfWords(String word){ | |
String[] words = word.split("\\s+"); | |
return (words.length ) ; | |
} | |
public static int findNumberOfLetters(String word){ | |
String[] words = word.split("\\s+"); | |
String sumOfWords = ""; | |
StringBuffer result = new StringBuffer(); | |
for(int i = 0; i < words.length; i++){ | |
result.append(words[i]); | |
} | |
sumOfWords = result.toString(); | |
return sumOfWords.length(); | |
} | |
public static void main(String[] args) throws FileNotFoundException{ | |
FileInputStream file = null; | |
JFileChooser chooseFile = new JFileChooser(); | |
int val = chooseFile.showOpenDialog(new JFrame()); | |
if(val == chooseFile.APPROVE_OPTION){ | |
File f = chooseFile.getSelectedFile(); | |
file = new FileInputStream(f.getAbsolutePath()); | |
} | |
String input = readFile(file); | |
System.out.println(input); | |
System.out.println("The number of words are : " + findNumberOfWords(input)); | |
System.out.println("The number of letters are: " + findNumberOfLetters(input)); | |
System.out.println("The number of symbols are: " + findNumberOfSymbols(input)); | |
printThreeMostCommonWords(input); | |
} | |
public static int findNumberOfSymbols(String word){ | |
int numOfSymbols = 0; | |
char[] charArray = word.toCharArray(); | |
for (int c = 0; c< charArray.length; c++){ | |
if ((charArray[c] >= '!' && charArray[c] <= '/') || (charArray[c] >= ':' && charArray[c] <= '@') || (charArray[c] >= '[' && charArray[c] <= '`') || (charArray[c] >= '{' && charArray[c] <= '~')){ | |
numOfSymbols++; | |
} | |
} | |
return numOfSymbols; | |
} | |
//this is where we use the FileInputStream to get the text from the selected file | |
private static String readFile(FileInputStream input) { | |
try{ | |
String result = ""; | |
int ch = 0; | |
while((ch =input.read() )!= -1){ | |
result += (char)(ch); | |
} | |
return result; | |
catch(Exception evt){ | |
JOptionPane.showMessageDialog(null, "I think you need to choose a file first", "InfoBox: " + null, JOptionPane.ERROR_MESSAGE); | |
return null; | |
} | |
} | |
public static void printThreeMostCommonWords(String word){ | |
word = word.toLowerCase(); | |
String[] words = word.split("\\s+"); | |
for(int i = 0; i < words.length; i++){ | |
words[i] = words[i].replaceAll("[^a-zA-Z0-9]", ""); | |
} | |
String[] removedDuplicates = new HashSet<String>(Arrays.asList(words)).toArray(new String[0]); | |
int[] removedDupCount = new int[removedDuplicates.length]; | |
System.out.println(); | |
String currentWord = ""; | |
int count = 0; | |
for(int i = 0; i < removedDuplicates.length; i++){ | |
currentWord = removedDuplicates[i]; | |
for(int j = 0; j < words.length; j++){ | |
if(currentWord.equals(words[j])){ | |
count++; | |
} | |
} | |
removedDupCount[i] = count; | |
count = 0; | |
} | |
System.out.println(); | |
int max = removedDupCount[0]; | |
int maxPlace = 0; | |
for(int i = 0; i < removedDupCount.length; i++){ | |
if(removedDupCount[i] > max){ | |
max = removedDupCount[i]; | |
maxPlace = i; | |
} | |
} | |
int second = Integer.MIN_VALUE; | |
int secondPlace = 0; | |
for(int i = 0; i < removedDupCount.length; i++){ | |
if(removedDupCount[i] > second && removedDupCount[i] < max){ | |
second = removedDupCount[i]; | |
secondPlace = i; | |
} | |
} | |
int third = Integer.MIN_VALUE; | |
int thirdPlace = 0; | |
for(int i = 0; i < removedDupCount.length; i++){ | |
if(removedDupCount[i] > third && removedDupCount[i] < second){ | |
third = removedDupCount[i]; | |
thirdPlace = i; | |
} | |
} | |
System.out.println("first: " + removedDuplicates[maxPlace]); | |
System.out.println("second: " + removedDuplicates[secondPlace]); | |
System.out.println("second: " + removedDuplicates[thirdPlace]); | |
} | |
} | |
//output: | |
//The number of words are : 3002 | |
//The number of letters are: 17195 | |
//The number of symbols are: 624 | |
//first: ut | |
//second: sed | |
//third: amet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment