Skip to content

Commit

Permalink
Create WordInspection.java
Browse files Browse the repository at this point in the history
  • Loading branch information
giusepped committed Aug 23, 2014
1 parent 662235f commit 3be27d8
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions Week9/WordInspection/wordinspection/WordInspection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

package wordinspection;

/**
*
* @author giuseppedesantis
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class WordInspection {
private List<String> words;
private final String vowels = "aeiouyäö";

public WordInspection(File file) throws Exception{
Scanner reader = null;
try{
reader = new Scanner(file, "UTF-8");
}catch(Exception e){
System.out.println("we could not find the file");
return;
}
words = new ArrayList<String>();

while(reader.hasNextLine()){
String line = reader.nextLine();
words.add(line);
}
}

public int wordCount(){
int wordCount = 0;
for(String w : words){
wordCount++;
}
return wordCount;
}

public List<String> wordsContainingZ(){
List<String> wordsContainingZ = new ArrayList<String>();
for(String w : words){
if(w.contains("z")){
wordsContainingZ.add(w);
}
}
return wordsContainingZ;
}

public List<String> wordsEndingInL(){
List<String> wordsEndingInL = new ArrayList<String>();
for(String w : words){
int lastIndex = w.length() - 1;
if(w.substring(lastIndex).equals("l")){
wordsEndingInL.add(w);
}
}
return wordsEndingInL;
}

public List<String> palindromes(){
List<String> palindromes = new ArrayList<String>();
for(String word : words){
if(word.equals(reverse(word))){
palindromes.add(word);
}
}
return palindromes;
}

private String reverse(String word){
String reverse = "";
int length = word.length() - 1;
for(int i = length; i >= 0; i--){
reverse += word.charAt(i);
}
return reverse;
}

public List<String> wordsWhichContainAllVowels(){
List<String> returnedString = new ArrayList<String>();
for(String word : words){
if(containsAllVowels(word)){
returnedString.add(word);
}
}
return returnedString;
}

private boolean containsAllVowels(String word){
for(char vowel : this.vowels.toCharArray()){
if(!word.contains(""+vowel)){
return false;
}
}
return true;
}

}

0 comments on commit 3be27d8

Please sign in to comment.