Wednesday, February 1, 2012

I need some help with my java code for a name, zip array?

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.



Code:



import java.util.Scanner;

import java.io.*;



public class Person

{

public static void main (String[] args) throws IOException

{



String Lname, Fname, Address, City, State, Phone, Zip;

Friends[] myList = new Friends[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.useDelimiter("\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);

Friends newGuy = new Friends(Fname, Lname, Address, City, State, Phone, izip);

myList [count] = newGuy;

count++;

}

for (int i = 0; i %26lt; myList.length; i++) {

System.out.println(myList[i]);

}

}

}



2nd file:



public class Friends



{ private String LastName, FirstName, Address, City, State, Phone;

private int Zip;



public Friends (String Last, String First, String Add, String Cit, String Stat, String Phon,

int zip)

{

LastName = Last;

FirstName = First;

Address = Add;

City = Cit;

State = Stat;

Phone = Phon;

Zip = zip;

}



//--------------------------------------鈥?br>
// Returns a string description of this Student object.

//--------------------------------------鈥?br>
public String toString()

{

String result;



result = LastName + ", " + FirstName + ", " + Address + ", " + City + ", " + State + ", " + Zip + ", " + Phone;



return result;

}

}



Addresses.txt



John Coto 11111 Long St. Albany OR 5594 293592934

Ron Esker 22222 Main St. Sweet Home OR 74554 104938158

Fran Gobel 33333 Gardens Eugene IL 73455 0992381238

George Hippler 66666 Shady Boston CA 74565 5928246786

Kelly Wantz 77777 Silver St. Los Angelos CA 74543 235235234

Amie Wilmer 88888 Shadows Dallas CA 63546 5435363636

Julio Fair 99999 Allen St. Concord MI 63535 5435345435

Lorrie Diana 12345 Grove St. Drubin MI 13134 4567476477

Clint Freda 67891 East Ave. Iago FL 64574 9876543219

Max Wyer 14567 Autos Jericho HW 59678 1234567891

Guy Smith 74634 Vanderbilt Derry CT 59952 3333333333

Max Yohe 93475 Ocean Drive Denver DE 58928 2222222222

Allan Meigs 34823 Portland Boulevard Bristol GA 58698 2385829945

Darren Pilon 58572 Long st. Boston AK 85285 2085917474

Ted Saffold 93756 Long St. Barry AK 59685 3905868495

Noreen Wark 23844 Main Boulevard Milan OK 59286 2085917474

Ashley Rosin 45363 East Ave. Reno OH 58685 5928586543

Cody Duwe 52525 Vanderbilt Romulus NH 58685 4955683759

Hugh Thode 49875 Gardens Salem NJ 69548 4353462563

Jessie Adamo 59285 Spoolean Portland PA 49855 4392582854

Darryl Rone 94832 East Ave. Richmond PA 52434 4967485844

Matt Doe 58252 Grove St. Sheffield NM 59284 1234567894

Jack Gump 44444 Spoolean Wilsonville MT 96834 293592934





Everything runs until I put more then 3 lines or so into the text file then it gives and error i'm not sure what I need to change or do to make it work



Exception in thread "main" java.lang.NumberFormatException: For input string: "5

928246786"

at java.lang.NumberFormatException.forInput鈥?br>
java:48)

at java.lang.Integer.parseInt(Integer.java:鈥?br>
at java.lang.Integer.parseInt(Integer.java:鈥?br>
at Person.main(Person.java:27)

Press any key to continue . . .I need some help with my java code for a name, zip array?
You are only allowed one Scanner open at a time. Having two Scanner open will not work.

No comments:

Post a Comment