String Manipulation The task of this program is to read numbers from data/Phone.
ID: 3823127 • Letter: S
Question
String Manipulation
The task of this program is to read numbers from data/Phone.txt and display appropriately. Right now the code just prints out the file’s contents line by line. You should create an arrayList of every phone number and return that to the caller. We have created output variable for you to insert each number into. So we will go through what you are given first. You can see the format of the phone numbers are the following:
Country code is before the first dash (1-3 digits)
Between the first and second dashes are the city code (or area code)
After the second dash is the phone number
5-8 digits
May or may not have dash in the middle
Your job is to separate the phone number String into 3 parts and print out as shown in the expected output.
Making an ArrayList of People
We will practice using more Arraylist to create another list of objects. This time it will be a list of person objects, ie people. First you will have to complete the class definition of a Person object. Then by utilizing an arrayList keep track of all the people listed in different files.
Task 2: Person.java
Take a look at Person.java. It has 3 private variables: name, age and location. Fill-in or fix all the methods inside this Class.
Task 3: People.java
This program works by reading information about each person inside the file. There are three traits for each person: name, age and location. You need to create a Person object pointed to by people array for each line in the input file. The expected output is created by calling accessor methods on each person object. You job is to just change the part that says fill-in. The rest of the code is there to make testing easier and to make sure you do everything in an expected way. The program should runs as is but you will see it prints out no useful information.
people1.txt has the following features:
First entry is the name of the person (one word)
Second entry is the age (one number)
Third entry is the location (one word)
people2.txt has more complicated data format:
Tab delimited
First entry can be multiple words separated by space
Second entry is a number for age
Last entry can also be multiple words for location
Give the method call to useDelimiter() to use tab character.
Do you need next() or nextLine() to handle multiple words?
Task 4: IORunner.java
Put in appropriate calls to People.java so it prints out exactly as the Expected output for each of the files.
package io;
import java.util.ArrayList;
public class IORunner {
public static void main(String[] args) {
// edit as necessary
//testPeople();
}
public static void testPeople() {
// fill in as necessary
}
}
package io;
import java.util.*;
import java.io.*;
public class People {
public ArrayList<Person> readFile() {
// this version asks for the file
System.out.print("Enter the file name: ");
Scanner kbd = new Scanner(System.in);
String filename = kbd.next();
return readFile(filename);
}
public ArrayList<Person> readFile(String filename) {
// create people array
ArrayList<Person> people = new ArrayList<Person>();
try {
Scanner input = new Scanner ( new FileReader(filename) );
while (input.hasNextLine()) {
/*
* Fill-in .. Do not touch the rest of the code
*/
// end of code to be filled in
}
input.close();
} catch ( NoSuchElementException e){
System.out.println(e);
} catch (FileNotFoundException e) {
System.out.println(e);
}
return people;
}
public void printPeople(ArrayList<Person> people) {
for (Person p : people) {
System.out.println(p);
}
}
dataphone.txt
+1-555-555-5555
+1-800-555-1212
+61-3-9527-9527
44-1289-555555
}
package io;
// CONTAINS SOLUTIONS -- JAR THIS
public class Person {
private String name;
private int age;
private String location;
public Person() {
this.name = "";
this.age = 0;
this.location = "";
}
public Person(String name, int age, String location) {
// Fill-in
}
public void setName(String name) {
// Fill-in
}
public String getName() {
// Fix
return "";
}
public void setAge(int age) {
// Fill-in
}
public int getAge() {
// Fix
return 0;
}
public void setLocation(String location) {
// Fill-in
}
public String getLocation() {
// Fix
return "";
}
// every object in java has a toString method. The @Override line
// is an annotation telling Java that the definition below should
// replace the default method.
// Fill in the definition below such that it returns a String that
// describes a particular Person.
@Override
public String toString() {
// fix
// return "a person";
return "";
}
}
package io;
import java.util.*;
import java.io.*;
public class PhoneNums {
public ArrayList<String> readPhoneNumbers() {
String filename = "data/phone.txt";
ArrayList<String> output = new ArrayList<String>();
try {
Scanner input = new Scanner ( new FileReader(filename) );
while (input.hasNextLine()) {
/*
* Fill-in
*/
System.out.println(input.nextLine()); // Comment this line out
}
input.close();
} catch ( NoSuchElementException e){
System.out.println(e);
} catch (FileNotFoundException e) {
System.out.println(e);
}
return output;
}
}
Explanation / Answer
//Person.java
package io;
public class Person {
private String name;
private int age;
private String location;
public Person() {
this.name = "";
this.age = 0;
this.location = "";
}
public Person(String name, int age, String location) {
this.name=name;
this.age=age;
this.location=location;
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return age;
}
public void setLocation(String location) {
this.location=location;
}
public String getLocation() {
return location;
}
public String toString() {
return String.format("%-10s%-5d%-10s ", name,age,location);
}
}
----------------------------------------------------------------------------------------------------------------------------
//PhoneNums.java
package io;
import java.util.*;
import java.io.*;
public class PhoneNums {
public ArrayList<String> readPhoneNumbers() {
String filename = "phone.txt";
ArrayList<String> output = new ArrayList<String>();
try {
Scanner input = new Scanner ( new FileReader(filename) );
while (input.hasNextLine()) {
//read string from file
String phoneNumber=input.nextLine();
//get index of character '-'
int pos=phoneNumber.indexOf("-");
//get country code
String cCode=phoneNumber.substring(0,pos);
phoneNumber=phoneNumber.substring(pos+1, phoneNumber.length());
pos=phoneNumber.indexOf("-");
//get area code
String aCode=phoneNumber.substring(0,pos);
//get phone code
String phoneCode=phoneNumber.substring(pos+1,phoneNumber.length());
//add phone number sub parts to the array list ,output
output.add(String.format("%-15s%-10s%-15s",cCode,aCode,phoneCode));
}
//close input object
input.close();
} catch ( NoSuchElementException e){
System.out.println(e);
} catch (FileNotFoundException e) {
System.out.println(e);
}
return output;
}
}
----------------------------------------------------------------------------------------------------------------------------
//IORunner.java
package io;
import java.util.ArrayList;
public class IORunner {
public static void main(String[] args) {
testPeople();
}
public static void testPeople() {
//create an instance of PhoneNums
PhoneNums phone=new PhoneNums();
//calling readPhoneNumbers method
ArrayList<String>phones=phone.readPhoneNumbers();
System.out.printf("%-15s%-10s%-15s ","Country","Area","PhoneCode");
//print phoneNumber from phones array list
for (String phoneNumber : phones) {
System.out.println(phoneNumber);
}
}
}
----------------------------------------------------------------------------------------------------------------------------
phone.txt
+1-555-555-5555
+1-800-555-1212
+61-3-9527-9527
44-1289-555555
----------------------------------------------------------------------------------------------------------------------------
Sample output:
Country Area PhoneCode
+1 555 555-5555
+1 800 555-1212
+61 3 9527-9527
44 1289 555555
Note :please post another question in next post