I need to be able to ask user for file name, then display the names formatted.
Write a java program that reads a sequence of names (first name followed by last name, separated by at least one space) from a text file and:
Displays the list of names (last name followed by a comma, followed by one space, followed by first name) that are read from the input file in ascending order. Each first name and last name must be capitalized (first letter in upper case and the remaining letters in lower case). First and last names must be lined up as per the example below.
For each first name in the file, displays the number of times that the first name appears in the file. (i.e. firstName: count). This list of first names must be displayed in the ascending order of the first names. Each first name must be capitalized (first letter in upper case and the remaining letters in lower case) and displayed only once. First names and counts must be lined up as per the example below.
For each last name in the file, displays the number of times that the last name appears in the file. (i.e. lastName: count). This list of last names must be displayed in the ascending order of the last names. Each last name must be capitalized (first letter in upper case and the remaining letters in lower case) and displayed only once. Last names must be lined up as per the example below.
For each name in the file, (last name followed by a comma, followed by one space, followed by first name) displays the number of times that the names appears in the file. (i.e. name: count). This list of names must be displayed in the ascending order of the names. All first and last names must be capitalized. Each name must be displayed only once. First names, last names and counts must be lined up as per the example below.
Displays the list of unique names (last name followed by a comma, followed by one or more spaces, followed by first name) in ascending order. First names and last names must be lined up as per the example below.
Writes the list of unique names (last name followed by a comma, followed by one or more spaces, followed by first name) in ascending order to a text file. First names and last names be lined up similar to display in item 5.
The program must ask the user to provide the names of the input and output files
The output should be:
Enter the name of the input file
input.txt
******* All Names *********
Beres, Kirsten
Beres, Kirsten
Beumer, Gretta
Hutt, Colette
Hutt, Shawanda
Jones, Colette
Jones, Marcia
Koenig, Gerri
Means, Tijuana
Montilla, Adriana
Montilla, Adriana
Montilla, Adriana
Montilla, Adriana
Mossman, Emmanuel
Sapienza, Colette
Sapienza, Colette
Shover, Neva
Stanfill, Marcia
******* First Names count*********
Adriana 4
Colette 4
Emmanuel 1
Gerri 1
Gretta 1
Kirsten 2
Marcia 2
Neva 1
Shawanda 1
Tijuana 1
******* Last Names count *********
Beres 2
Beumer 1
Hutt 2
Jones 2
Koenig 1
Means 1
Montilla 4
Mossman 1
Sapienza 2
Shover 1
Stanfill 1
******* All Names count*********
Beres, Kirsten 2
Beumer, Gretta 1
Hutt, Colette 1
Hutt, Shawanda 1
Jones, Colette 1
Jones, Marcia 1
Koenig, Gerri 1
Means, Tijuana 1
Montilla, Adriana 4
Mossman, Emmanuel 1
Sapienza, Colette 2
Shover, Neva 1
Stanfill, Marcia 1
******* All Unique Names *********
Beres, Kirsten
Beumer, Gretta
Hutt, Colette
Hutt, Shawanda
Jones, Colette
Jones, Marcia
Koenig, Gerri
Means, Tijuana
Montilla, Adriana
Mossman, Emmanuel
Sapienza, Colette
Shover, Neva
Stanfill, Marcia
Enter the name of the output file
output.txt
This is what I have so far but I feel lost, cannot find a way to capitalize and count. I have tried several things but nothing seems to work.
public static void getNames(ArrayList<String> fn,
ArrayList<String> ln) throws IOException {
Scanner kb = new Scanner(System.in);
System.out.print("What is the name input file? ");
String fileName = kb.next();
File inpFile = new File(fileName);
Scanner in = new Scanner(inpFile);
while (in.hasNext()) {
String firstName = in.next();
String lastName = in.next();
fn.add(firstName);
ln.add(lastName);
}
}
public static void display(ArrayList<String> names) {
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
}
public static void capitalize(ArrayList<String> firstName) {
for (int i = 0; i < firstName.size(); i++) {
firstName.set(i; toCapital(firstName.get(i)));
}
}
/*
* public static void capitalize(ArrayList<String> names) {
* for (int i = 0; i < names.size(); i++){
* names.set(i,toCapital(names.get(i)));
* }
* }
*/
//public static String toCapital (String name){
//String.toUpperCase(name.charAt(0)) +
//String.toLowerCase(name.substring(1));
//return j;
//}
public static void main(String[] args) throws IOException {
// TODO code application logic here
ArrayList<String> first = new ArrayList<>();
ArrayList<String> last = new ArrayList<>();
getNames(first, last);
//display(first);
//display(last);
ArrayList<String> allNames = new ArrayList<>();
for (int i = 0; i < first.size(); i++) {
allNames.add(last.get(i) + ", " + first.get(i));
}
display(allNames);
}
This is the content of the input file:
colette Sapienza gretta Beumer EMManuel Mossman Colette Sapienza marcia Jones Shawanda Hutt Adriana monTILla adriana montilla Adriana Montilla Colette Jones Colette Hutt Marcia Stanfill NeVa shover tijuana Means Adriana Montilla gerri KoeNig Kirsten beres Kirsten Beres
You can count the occurrences of each name using a HashMap
, e.g. for first names:
HashMap<String, Integer> firstNameCountMap = new HashMap<String, Integer>();
for (String firstName : first) {
if (firstNameCountMap.containsKey(firstName))
firstNameCountMap.put(firstName, firstNameCountMap.get(firstName)++);
else
firstNameCountMap.put(firstName, 0);
}
System.out.println("******* First Names count*********");
for (String firstName : firstNameCountMap.keySet()) {
System.out.println(firstName + " " + firstNameCountMap.get(firstName));
}
Then repeat for last names and all names. To capitalize the first character in a String see here, but essentially:
String capitalisedFirstName = firstName.substring(0, 1).toUpperCase() + firstName.substring(1);
EDIT
Since you need to put this functionality into predefined methods find(String s, ArrayList<String> a)
and capitalize(ArrayList<String> names)
you'll have to do a linear search in find, e.g.:
public static int find(String s, ArrayList<String> a) {
int count = 0;
for (String str : a) {
if (str.equalsIgnoreCase(s))
count++;
}
return count;
}
and to edit the collection of names to capitalized versions:
public static void capitalize(ArrayList<String> names) {
for (int i=0; i<names.size(); i++) {
String capitalizedName = names.get(i).substring(0, 1).toUpperCase() + names.get(i).substring(1);
names.set(i, capitalizedName);
}
}