In: Computer Science
Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals (sample input data is attached). Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate format to the screen. It should also Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-'. Need to Store the data in an ArrayList object
import java.util.*;
class data
{
String name1,name2,street,city,state;
int zip;
long phone;
data()
{
Scanner sc = new Scanner(System.in);//to read input
System.out.print("Enter two names:");
name1=sc.nextLine();
name2=sc.nextLine();
System.out.print("Enter zip code:");
zip = sc.nextInt();
System.out.print("Enter street name:");
street = sc.nextLine();
street = sc.nextLine();
System.out.print("Enter city name:");
city =sc.nextLine();
System.out.print("Enter state name:");
state = sc.nextLine();
System.out.print("Enter phone number:");
phone = sc.nextLong();
}
void print()
{
System.out.println("Name1 :"+name1+"\nName2 :"+name2+"\nZip
:"+zip+"\nstreet :"+street+"\ncity :"+city+"\nstate
:"+state+"\nphone :"+phone);
}
}
public class Main
{
public static void main(String[] args) {
System.out.println("Enter number of
values you want to store:");
int n;
Scanner sc = new
Scanner(System.in);//to readinput
n=sc.nextInt();
ArrayList<data> a = new
ArrayList<data>();//storing values in ArrayList
int i=0;
while(i<n)
{
data d = new data();
a.add(d);//adding to
arraylist
i++;
}
System.out.println("----Displaying
List---------");
i=0;
while(i<n)
{
a.get(i).print();
i++;
}
//displaying data
}
}
output:
Enter number of values you want to store:
2
Enter two names:surya
saikam
Enter zip code:120003
Enter street name:rice mill
Enter city name:turbun
Enter state name:njy
Enter phone number:1234567890
Enter two names:phani
k
Enter zip code:12003
Enter street name:R j
Enter city name:basti
Enter state name:Ap
Enter phone number:9876543211
----Displaying List---------
Name1 :surya
Name2 :saikam
Zip :120003
street :rice mill
city :turbun
state :njy
phone :1234567890
Name1 :phani
Name2 :k
Zip :12003
street :R j
city :basti
state :Ap
phone :9876543211