0 Replies - 6285 Views - Last Post: 29 April 2012 - 01:37 PM Rate Topic: -----

#1 jediv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-October 11

Form Letter creation with String Arrays and String replacement

Posted 29 April 2012 - 01:37 PM

HOMEWORK QUESTION!!!
Ok, I have to admit at this time, I just don't quite know what to do on the particulars of the homework I have.
First, In a class that is named FormLetter, I am reading in a external text file into an array called 'linelist' that holds a form letter. This letter has within it %FIRST, %LAST, %TOWN.
Second, I am also reading in from a separate text file called names into 2(two) separate arrays on that holds a key value the other that holds the list values.

This is what the letter.txt looks like

Dear %FIRST,
As a person who needs to stay well informed, you, %FIRST
%LAST of %TOWN, cannot afford to be without a subscription
to YESTERDAY'S NEWS. If you subscribe then as you walk down
the main street of %TOWN people will say "There goes %FIRST
%LAST, the best informed person in %TOWN." Wouldn't you like
that, %FIRST? Please, %FIRST %LAST, subscribe to YESTERDAY'S
NEWS right away.

This is what the names.txt file looks like


3<-this is my keylist
%FIRST %LAST %TOWN<- this is my vallist header and then the next two rows are the values themselves
Joseph Kool Cranston
Mary Perfect Lincoln

As the letter is read in thru the Scanner into the array called linelist I can get it to print out to the screen in a test process but honestly after that I am just not getting it. Now I know most out there will say for me to ask the professor, I have and honestly he is not the most willing to be helpful on this because he feels we have everything we need to get this figured out. I am just having a tough time getting thru the process of arrays and doing things like this. If anyone can help that would be great. I need to step through this a method at a time. If anyone can afford that I would greatly appreciate it. I have to conform to how my professor wants it coded. I just need to get this into my head, I am just a tough nut to crack when it comes to Java.

This is the document he gave us to work from and to build up from. I want to understand the pieces. I need help not a handout of answers, it does me no good if I can't figure out what I am writing. Thanks in advance.

Write a program that personalizes a form letter for each client
on a mailing list. This is done by replacing all occurrences of
certain keys in the form letter with corresponding values read
from a mailing list.

The form letter is read from the text file LETTER.TXT. It
contains the text which must be personalized for each client
on the mailing list. Use the following text:

Dear %FIRST,
As a person who needs to stay well informed, you, %FIRST
%LAST of %TOWN, cannot afford to be without a subscription
to YESTERDAY'S NEWS. If you subscribe then as you walk down
the main street of %TOWN people will say "There goes %FIRST
%LAST, the best informed person in %TOWN." Wouldn't you like
that, %FIRST? Please, %FIRST %LAST, subscribe to YESTERDAY'S
NEWS right away.


The mailing list is read from the text file NAMES.TXT. The first
line contains the number of keys which will be replaced in the
form letter. The second line contains the keys as they appear
in the form letter. (You may assume that each key contains no
white space.) Each remaining line contains the values to be
substituted for the corresponding keys for a specific client.
(You may assume that each value contains no white space.) Use
the following file:

3
%FIRST %LAST %TOWN
Joseph Kool Cranston
Mary Perfect Lincoln

The output should be a personalized form letter for each client:

Dear Joseph,
As a person who needs to stay well informed, you, Joseph
Kool of Cranston, cannot afford to be without a subscription
to YESTERDAY'S NEWS. If you subscribe then as you walk down
the main street of Cranston people will say "There goes Joseph
Kool, the best informed person in Cranston." Wouldn't you like
that, Joseph? Please, Joseph Kool, subscribe to YESTERDAY'S
NEWS right away.

Dear Mary,
As a person who needs to stay well informed, you, Mary
Perfect of Lincoln, cannot afford to be without a subscription
to YESTERDAY'S NEWS. If you subscribe then as you walk down
the main street of Lincoln people will say "There goes Mary
Perfect, the best informed person in Lincoln." Wouldn't you like
that, Mary? Please, Mary Perfect, subscribe to YESTERDAY'S
NEWS right away.

Your program must use the classes specified below. To receive
full credit your program must follow the specifications exactly.

The FormLetter class stores the form letter. The "size" field
holds the number of lines in the form letter. The lines are
held in the array elements linelist[0] through linelist[size-1].

import java.io.*;
import java.util.*;

public class FormLetter {
private String[] linelist;
private int size;

public FormLetter(int max) {
// Initialize an empty form letter that can hold up
// to "max" lines. Allocate the "linelist" array
// and initialize size to zero.
}

public void read(Scanner letter) {
// Read the form letter from the scanner passed as the
// argument. Read the lines of the form letter into
// the "linelist" array and set "size" to the number
// of lines read.
}

public void personalize(Client clnt, PrintWriter out) {
// Personalize the form letter for the client data
// passed as the argument "clnt". For each line of
// the form letter, call "clnt.format" to personalize
// the line. Then write the personalized line to "out".
}

public void processMailing(Scanner names, PrintWriter out) {
// Process the mailing list file from "names". Read
// the number of keys from the first line of the file.
// Then read the keys into a local array of strings.
// Process the remaining lines as client data. Create
// a Client object for each client and call the
// personalize method to write the letter to "out".
}
}


The Client class represents the specific substitutions of
values for keys for a specific client. The "size" field holds
the number of key-value pairs. The keys are stored in keylist[0]
through keylist[size-1]. The corresponding values are stored in
vallist[0] through vallist[size-1].


public class Client {
private String[] keylist;
private String[] vallist;
private int size;

public Client(int max) {
// Initialize an empty object that can hold up to "max"
// key-value pairs. Allocate the "keylist" and "vallist"
// arrays and initialize size to zero.
}

public void add(String key, String val) {
// Add the specified key-value pair. Insert "key" and
// "val" into the first unoccupied array elements and
// add one to "size".
}

public String format(String str) {
// Return the string obtained by replacing each occurrence
// of every key in the string passed as the argument with
// the corresponding value. (Use the "replace" method of
// the built-in String class to replace each occurrence
// of a single key with the corresponding value. Repeat
// this for each key.)
}
}


The Program3 class contains the main method. Use the following
code:


import java.io.*;
import java.util.*;

public class Program3 {
public static void main(String[] args) {
FormLetter form;
Scanner letter = null;
Scanner names = null;
PrintWriter out = null;

try {
// Open the input/output streams.
letter = new Scanner(new FileReader("LETTER.TXT"));
names = new Scanner(new FileReader("NAMES.TXT"));
out = new PrintWriter(System.out, true);
} catch (IOException exc) {
// Exit if a stream cannot be opened.
System.out.println(exc);
System.exit(1);
}

// Create the form letter object.
form = new FormLetter(100);
form.read(letter);
letter.close();

// Process the mailing list.
form.processMailing(names, out);
names.close();
}
}


Add comments to your code that explain the purpose of each class
and method. Hand in the code for all classes and the output from
a run.


import java.io.PrintWriter;
import java.util.Scanner;

public class FormLetter {
	private String[] linelist;
	private int size;

	public FormLetter(int max) {
		size = 0;
		linelist = new String[0];
	}

	public void read(Scanner letter) {

		while (letter.hasNextLine()) {
			String linelist = letter.nextLine();
			++size;
		}
	}

	public void personalize(Client clnt, PrintWriter out) {

		for (int i = 0; i < size; ++i) {
			clnt.format(linelist[0]);
			out.print(linelist[0]);
		}

	}

	public void processMailing(Scanner names, PrintWriter out) {

	}

}



import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Program9 {

	public static void main(String[] args) {
		FormLetter form;
		Scanner letter = null;
		Scanner names = null;
		PrintWriter out = null;

		try {
			// Open the input/output streams
			letter = new Scanner(new FileReader("LETTER.TXT"));
			names = new Scanner(new FileReader("NAMES.TXT"));
			out = new PrintWriter(System.out, true);
		} catch (IOException exc) {
			// Exit if a stream cannot be opened.
			System.out.println(exc);
			System.exit(1);
		}

		// Create the form letter object.
		form = new FormLetter(100);
		form.read(letter);
		letter.close();

		// process the mailing list.
		form.processMailing(names, out);
		names.close();
	}
}



public class Client {
	private String[] keylist;
	private String[] vallist;
	private int size;

	public Client(int max) {
		size = 0;
		keylist = new String[0];
		vallist = new String[0];
	}

	public void add(String key, String val) {
		
		size++;
	}

	public String format(String str) {

	}
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1