the directions are to
Create a text file that contains 25 lines - each line having a first name (string), last name (string), address (string), city (string), state (string), zip code (integer) and phone number (string) separated by tabs. Save that text file to be used in your program.
In your program, read each line of your file as a string. Then break each line into last name, first name, address, city, state and zip code. Instantiate an object with those values, put that object into an array. You will need to create a class which contains First name, Last name, Address, City, State, Zip code and Phone number as instance variables. Create all appropriate methods for this class.
So far I have
import java.util.Scanner;
import java.io.*;
public class Person
{
public static void main (String[] args)
{
String Lname, Fname, Address, City, State, Phone, Zip;
Person[] myList = new Person[25];
int count = 0;
File myFile = new File ("Addresses.txt");
Scanner FileScan = new Scanner (myFile);
String Line;
while (FileScan.hasNext())
{
Line = FileScan.nextLine();
Scanner LineScan = new Scanner(Line);
LineScan.useDelimeter("\t");
Fname = LineScan.next();
Lname = LineScan.next();
Address = LineScan.next();
City = LineScan.next();
State = LineScan.next();
Phone = LineScan.next();
Zip = LineScan.next();
int izip = Integer.parseInt(Zip);
Person newGuy = new Person(Fname, Lname, Address, City, State, Phone, Zip);
myList [count] = newGuy;
count++;
}
}
}
Beyond this im stuck as to what I need to do to create the array and store it into each element in the arrayName, Zip Array java code?
// You need a driver class with the main()
// the driver class makes the Objects
// Objects are in RAM, they come from class files that makes many copies
//
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class AddressBook {
public People[] fockers;
public static void main(String[] args) {
// I don't want static methods
// I go ahead and make an Object out of
// the main program
new AddressBook().run();
}
private void run() {
String filename = "data.txt";
// make sure your text file is in same folder as this program
File file = new File(System.getProperty("user.dir"), filename);
// show the path to the filename
System.out.println(file.getPath());
FileReader fin = null;
Scanner sc = new Scanner(fin);
StringBuilder accumulator = new StringBuilder();
String dataSplitter = "#$#";
try {
fin = new FileReader(filename);
while (sc.hasNext()) {
accumulator.append(sc.nextLine());
accumulator.append(dataSplitter);
}
} catch (FileNotFoundException ex) {
System.out.println("cound not find the file!");
} finally {
try {
fin.close();
} catch (IOException ex) {
System.out.println("tried to close the FileReader");
}
}
makeArray(accumulator, dataSplitter);
} // end method run()
private void makeArray(StringBuilder sb, String delimeter) {
String[] temp = sb.toString().split(delimeter);
fockers = new People[ temp.length ];
for (int i = 0; i %26lt; temp.length; i++) {
fockers[i] = new People( temp[i] );
}
for (int i = 0; i %26lt; fockers.length; i++) {
System.out.println(fockers[i]);
}
}
//=================== inner class
// this could be a separate file, but let's keep it simple
// inner classes expose all the variables, which can be
// good or bad
class People {
// Objects are UpperCase,
// variables are humpBack
// constructor
public People( String lineOfData) {
String [] flds = lineOfData.split("\t");
lName = flds[0];
fName = flds[1];
address = flds[2];
city = flds[3];
state = flds[4];
phone = flds[5];
zip = Integer.parseInt( flds[6]);
flds = null;
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getfName() {
return fName;
}
public String getlName() {
return lName;
}
public String getPhone() {
return phone;
}
public String getState() {
return state;
}
public int getZip() {
return zip;
}
public String toString() {
return new StringBuilder()
.append(fName)
.append(" " )
.append(lName)
.append( "\n")
.append(address)
.append("\n")
.append(city)
.append(", ")
.append(state)
.append(" " )
.append(zip)
.append("\n")
.toString();
}
private String lName;
private String fName;
private String address;
private String city;
private String state;
private String phone;
private int zip;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment