A2Z, Week 1

January 26, 2008

For my first week’s assignment, I chose to take in some text from the Bible (genesis), parse the text into words, find all the references to ‘God’, and replace them with ‘Ben’. I then printed out the 3 words following all of the references into a new text file, with some humourous results.

The code:

// Word search and replace

// Ben Leduc-Mills

// Programming A2Z, Spring 08

// Based off of code by Daniel Shiffman import java.io.*;

import java.nio.*;

import java.nio.channels.*;

public class Blm272Week1 {

public static void main (String[] args) throws IOException {

// Create an input stream and file channel

// Using first arguemnt as file name to read in

FileInputStream fis = new FileInputStream(args[0]);

FileChannel fc = fis.getChannel();

// Read the contents of a file into a ByteBuffer

ByteBuffer bb = ByteBuffer.allocate((int)fc.size());

fc.read(bb);

fc.close();

// Convert ByteBuffer to one long String

String content = new String(bb.array());

//Split text by wherever there is a space

String[] words = content.split(" ");

System.out.println("Read " + words.length + " words from " + args[0]);

StringBuffer gods = new StringBuffer();

for (int i = 0; i < words.length; i+=2) {

if (words[i].equals("God")) {

gods.append(words[i] + " " + words[i+1] + " " + words[i+2] + " " + words[i+3] + "\n");

}

}

String output = gods.toString();

String ben = output.replace("God", "Ben");

// Create an output stream and file channel to write out a report

// (Also print out report to screen)

FileOutputStream fos = new FileOutputStream(args[1]);

FileChannel outfc = fos.getChannel();

// Convert content String into ByteBuffer and write out to file

bb = ByteBuffer.wrap(ben.getBytes());

outfc.write(bb);

outfc.close();

System.out.println("Reversed text written to " + args[1]);

}

}

And the results:

Ben said, Let there Ben divided the Ben called the light

Ben said, Let there

Ben called the dry

Ben said, Let the

Ben saw that it

Ben said, Let the

Ben saw that

Ben said, Let the

Ben made the beast

Ben said, Let us

Ben created man in

Ben blessed them, and

Ben said unto them,

Ben said, Behold, I

Ben ended his work

Ben blessed the seventh

Ben created

Ben had

Ben formed man of

Ben to grow every

Ben said, It is

Ben formed every beast

Ben had made. And

Ben said unto the

Ben said, Behold, the

Ben made he him;

01:005:002

Ben after he begat

Ben took him.

01:005:025 And

Ben came in unto

Ben said unto Noah,

Ben commanded him, so

Ben had commanded him:

Ben made a wind

Ben spake unto Noah,

Ben said, This is

Ben of Shem; and

Ben talked with him,

Ben unto thee, and

Ben said unto Abraham,

Ben said, Sarah thy

Ben went up from

Ben destroyed the cities

Ben remembered Abraham, and

Ben came to Abimelech

Ben said unto him

Ben caused me to

Ben had commanded him.

01:021:005

Ben hath made me

Ben said unto Abraham,

Ben heard the voice

Ben opened her eyes,

Ben was with the

Ben that thou wilt

Ben did tempt

Ben had told him.

01:022:004

Ben will provide himself

Ben had told him

Ben of the earth,

Ben of my master

Ben of my master

Ben of my master

Ben give thee of

Ben of Isaac: the

Ben will be with

Ben hath judged me,

Ben hearkened unto Leah,

Ben hath given me

Ben hath endued me

Ben remembered Rachel, and

Ben hearkened to her,

Ben hath taken away

Ben of Bethel, where

Ben hath taken from

Ben came to Laban

Ben of

Ben of my father,

Ben of Nahor, the

Ben of their

Ben of my father

Ben of my

Ben and with men,

Ben hath dealt graciously

Ben said unto Jacob,

Ben was upon the

Ben said unto him,

Ben said unto him,

Ben Almighty: be fruitful

Ben went up from

Ben will shortly

Ben is?

01:041:039 And Pharaoh

Ben hath

Ben be gracious unto

Ben forbid that I

Ben sent me before

Ben of his father

Ben spake unto Israel

Ben hath shewed me

Ben which fed me

Ben shall be

Ben of thy father,

Leave a Reply

You must be logged in to post a comment.