This week’s A–Z assignment:
Create a program (using, e.g., the tools presented in class) that behaves like a UNIX text processing program (such as cat, grep, tr, etc.). Your program should take text as input (any text, or a particular text of your choosing) and output a version of the text that has been filtered and/or munged. Your program should use at least one method of Java’s String class that we didn’t discuss in class.
Be creative, insightful, or intentionally banal. Optional: Use the program that you created in tandem with another UNIX command line utility
Sounds easy-peasy, except that despite our handy review session, I’ve completely forgotten anything I ever knew about Java or Processing. Not good.
Finally, after hours of beating my head against the couch and various O’Reilly books, I came up with two Java versions of the vowel shift–inducing UNIX command tr 'AEIOUaeiou' 'EIOUAeioua'
. The first loops through the input letter by letter, checking each to see if it belongs to an array of vowels:
[sourcecode language=’java’]import com.decontextualize.a2z.TextFilter;
public class Tr extends TextFilter
{
public static void main(String[] args)
{
Tr t = new Tr();
t.run();
} // end main(String[] args)
public void eachLine(String line)
{
char [] vowels_in = new char [] { ‘A’, ‘a’, ‘E’, ‘e’, ‘I’, ‘i’, ‘O’, ‘o’, ‘U’, ‘u’ };
char [] vowels_out = new char [] { ‘E’, ‘e’, ‘I’, ‘i’, ‘O’, ‘o’, ‘U’, ‘u’, ‘A’, ‘a’ };
for (int i = 0; i < line.length(); i++)
{
char currentletter;
char currentvowel;
char printletter;
currentletter = line.charAt(i);
printletter = currentletter;
for (int v = 0; v < 10; v++)
{
currentvowel = vowels_in[v];
if (currentletter == currentvowel)
{
printletter = vowels_out[v];
} // end if
} // end for v loop
print(printletter);
} // end for i loop
} // end eachLine(String line)
} // end Tr extends TextFilter[/sourcecode]
The second rather tediously searches for and replaces each vowel in turn:
[sourcecode language='java']import com.decontextualize.a2z.TextFilter;
public class Tr2 extends TextFilter
{
public static void main(String[] args)
{
Tr2 t = new Tr2();
t.run();
} // end main(String[] args)
public void eachLine(String line)
{
String line_new;
line_new = line.replace( "a", "[1]" );
line_new = line_new.replace( "e", "[2]" );
line_new = line_new.replace( "i", "[3]" );
line_new = line_new.replace( "o", "[4]" );
line_new = line_new.replace( "u", "[5]" );
line_new = line_new.replace( "A", "[6]" );
line_new = line_new.replace( "E", "[7]" );
line_new = line_new.replace( "I", "[8]" );
line_new = line_new.replace( "O", "[9]" );
line_new = line_new.replace( "U", "[10]" );
line_new = line_new.replace( "[1]", "e" );
line_new = line_new.replace( "[2]", "i" );
line_new = line_new.replace( "[3]", "o" );
line_new = line_new.replace( "[4]", "u" );
line_new = line_new.replace( "[5]", "a" );
line_new = line_new.replace( "[6]", "E" );
line_new = line_new.replace( "[7]", "I" );
line_new = line_new.replace( "[8]", "O" );
line_new = line_new.replace( "[9]", "U" );
line_new = line_new.replace( "[10]", "A" );
print(line_new + "\n");
} // end eachLine(String line)
} // end Tr2 extends TextFilter[/sourcecode]
I tried to rewrite the latter using arrays and a loop, as well---
[sourcecode language='java']import com.decontextualize.a2z.TextFilter;
public class TrB extends TextFilter
{
public static void main(String[] args)
{
TrB t = new TrB();
t.run();
} // end main(String[] args)
public void eachLine(String line)
{
String line_new;
String find_string;
String replace_string;
char [] vowels_in = new char [] { 'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u' };
char [] vowels_out = new char [] { 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', 'A', 'a' };
line_new = line;
for (int i = 0; i < vowels_in.length; i++)
{
find_string = "[" + i + "]";
line_new = line_new.replace( vowels_in[i], find_string );
}
for (int j = 0; j < vowels_in.length; j++)
{
replace_string = "[" + j + "]";
line_new = line_new.replace( replace_string, vowels_out[j] );
}
print(line_new + "\n");
} // end eachLine(String line)
} // end Tr2 extends TextFilter[/sourcecode]
but I just couldn't get past this error message:
rB.java:28: cannot find symbol symbol : method replace(char,java.lang.String) location: class java.lang.String line_new = line_new.replace( vowels_in[i], find_string ); ^ TrB.java:33: cannot find symbol symbol : method replace(java.lang.String,char) location: class java.lang.String line_new = line_new.replace( replace_string, vowels_out[j] ); ^ 2 errors
What stupid, obvious thing am I doing wrong?
Sample Input
Nelson was enjoying a little quiet at his house in the pretty village of Merton, in Surrey, when he learnt that the French and Spanish fleet, joined by the Ferrol squadron, had succeeded in entering Cadiz. His resolution was quickly taken. He went to the Admiralty and offered his services, which were joyfully accepted. Nelson was full of hope. “Depend on it,” he said to captain Blackwood, “I shall yet give M. Villeneuve a drubbing.” He formed his plans of attack during the short time of preparation, when the Victory had to be refitted, and other ships were to be got ready to accompany him. Lord Sidmouth told Mr. Eush, the American ambassador, that in the course of a visit he had received from Nelson, three weeks before the battle of Trafalgar, he described the plan of it, with bits of paper on a table, as it was afterwards fought. Yet he had a presentiment of his own fate. The coffin which was made out of the mast of l’Orient was deposited at an upholsterer’s. He desired its history to be engraved on its lid, saying that he should probably want it on his return. When he arrived at Portsmouth on the 14th of September, the enthusiasm of the people reached that height which sometimes gives a character of sublimity to the movements of multitudes acting with one heart. They wept; they blessed him; they even knelt as he passed along. The cheer which went up from thousands of voices as his barge pushed off to his flag-ship, -was the Godspeed of hia country. He waved his hat—a last farewell to England.
—The Popular History of England: An Illustrated History of Society and Government from the Earliest Period to Our Own Times by Charles Knight (Boston: Estes and Lauriat, 1874)
Sample Output
Nilsun wes injuyong e lottli qaoit et hos huasi on thi pritty vollegi uf Mirtun, on Sarriy, whin hi liernt thet thi Frinch end Spenosh fliit, juonid by thi Firrul sqaedrun, hed sacciidid on intirong Cedoz. Hos risulatoun wes qaockly tekin. Hi wint tu thi Edmorelty end uffirid hos sirvocis, whoch wiri juyfally ecciptid. Nilsun wes fall uf hupi. “Dipind un ot,” hi seod tu cepteon Bleckwuud, “O shell yit govi M. Volliniavi e drabbong.” Hi furmid hos plens uf etteck darong thi shurt tomi uf priperetoun, whin thi Voctury hed tu bi rifottid, end uthir shops wiri tu bi gut riedy tu eccumpeny hom. Lurd Sodmuath tuld Mr. Iash, thi Emirocen embessedur, thet on thi cuarsi uf e vosot hi hed riciovid frum Nilsun, thrii wiiks bifuri thi bettli uf Trefelger, hi discrobid thi plen uf ot, woth bots uf pepir un e tebli, es ot wes eftirwerds fuaght. Yit hi hed e prisintomint uf hos uwn feti. Thi cuffon whoch wes medi uat uf thi mest uf l’Uroint wes dipusotid et en aphulstirir’s. Hi disorid ots hostury tu bi ingrevid un ots lod, seyong thet hi shuald prubebly went ot un hos ritarn. Whin hi errovid et Purtsmuath un thi 14th uf Siptimbir, thi inthasoesm uf thi piupli riechid thet hioght whoch sumitomis govis e cherectir uf sablomoty tu thi muvimints uf maltotadis ectong woth uni hiert. Thiy wipt; thiy blissid hom; thiy ivin knilt es hi pessid elung. Thi chiir whoch wint ap frum thuasends uf vuocis es hos bergi pashid uff tu hos fleg-shop, -wes thi Gudspiid uf hoe cuantry. Hi wevid hos het—e lest feriwill tu Inglend.
—Thi Pupaler Hostury uf Inglend: En Ollastretid Hostury uf Sucoity end Guvirnmint frum thi Ierloist Piroud tu Uar Uwn Tomis by Cherlis Knoght (Bustun: Istis end Learoet, 1874)