Week 1 - Strings in Java
After getting several days frustrated with the Eclipse and also with Java Language, I finally gave my first steps in working with Strings in Java.
For this assignment, I did something very simple. Using one of the txt.files that were on the CVS ( bible.txt -- but it's working with any txt.file), I started to extract from the txt file the word I and the following word. In the middle of this 2 words I put the word don't just for fun.
String[] words = content.split("\\b");
// create a new string buffer ( that will store the words I + don't + nextword )
StringBuffer findI = new StringBuffer();
for ( int i=0; i< words.length;i++){
String find = "I";
if(words[i].equals(find)){
findI.append(words[i]+ " don't " + words[i+2] + " " + "\n");
}
}
String output1 = findI.toString();
The result was the String output1 that contains this (excerpt) :
I don't have
I don't have
I don't will
I don't heard
I don't was
I don't was
I don't hid
I don't commanded
I don't did
I don't did
I don't will
I don't will
I don't commanded
I don't have
I don't know
I don't my
I saw that some phrases were repeating, so the next step was to avoid that the same phrase appeared several times or more that once. I started by splitting that String (output1) into an Array phrase and then, using two for loops, comparing each elements of that Array with the rest of the elements. If the element of the Array ( that was been compare to the rest) was different , it was appended to the String Buffer finali
The trick that I use for this, was to create an empty String finali2 that allowed me to see if that element was already in the String Buffer finali. If it was, it wasn't appended, since it was already there.
String [] phrases = output1.split("\n");
// Create new String Buffer ( to each the unique phrases will be appended)
StringBuffer finali = new StringBuffer();
// temp String to compare content ( use contains -- cannot use any method for String Buffer?)
String finali2= "";
for ( int b=0; b
for(int c = b+1; c
finali.append(Temp + "\n");
finali2 = finali.toString();
}
}
}
String output = finali2;
See the result txt file here and to see all the code, click here


