Directions:
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.
public class Lab2
{
public static void main (String[] args) throws IOException
{
String[] stuff = new String[25];
Scanner fileScan, scoresScan;
fileScan = new Scanner (new File("Lines.txt"));
while (fileScan.hasNextLine())
{
strokes = fileScan.nextLine();
scoresScan = new Scanner(strokes);
p1score += scoresScan.nextLine();
This as as far as ive gotten i need a little help with what to do nextI need some help with my programming code?
// create an array to hold all the objects
Person[] people = new Person[25]
// create a reader
Scanner fileScan = new Scanner(new File("lines.txt"));
// read through the file
int lineNumber = 0;
while (fileScan.hasNextLine())
{
strokes = fileScan.nextLine();
// split up the read line by tabs
String[] data = strokes.split("\t");
// instantiate the object with those values (don't forget to make the constructor)
Person obj = new Person(data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
people[lineNumber] = obj;
lineNumber++;
}
Make a class with the specified variables. This should be in another file:
public class Person{
String first_name, last_name, address, city, state;
int zip_code, phone_number;
// add a constructor here
/* Create getters and setters here. */
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment