Reputation: 62
I'm stuck with this matter. I am trying to add new info to the "old" info in an ArrayList. I have tried a few different ways, but I can't get it to stay there.
import java.sql.Statement;
import java.util.*;
public class Head {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String lname, fname, y, m, d, x, dob, phone, place = null, info;
int choice;
Date today = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(today.getTime());
ArrayList<Person>plist = new ArrayList<>();
ArrayList<Faktura>flist = new ArrayList<>();
Person p1 = new Person("Shaerer", "Lucas", "200307302232", "0702-523219", "Stockholm", "Her we put some info");
Person p2 = new Person("Smith", "Jacob", "197609071464", "0761-617163", "Miami", "Some information about this person");
plist.add(p1);
plist.add(p2);
case 2:
System.out.println("Add info");
System.out.println("------------------------------------------------------");
System.out.println(String.format("%-15s%-15s", p.getLname(), p.getFname()));
System.out.println("------------------------------------------------------");
System.out.println(String.format("DateOfBirth: "+ p.getDob()));
System.out.println(String.format("Adress: "+ p.getPlace()));
System.out.println(String.format("Phone: "+ p.getPhone()));
System.out.println(String.format("Info: \n"+ p.getInfo()));
System.out.println("\n" + sqlDate);
System.out.println("Info: ");
info = scan.nextLine();
// plist.add(this);
// for(int i = 0;i<plist.size(); i++){
// List<Person>plist = Arrays.asList(plist);
// for (Person s : plist)
// plist.add(s);
break;
Constructor
public class Person {
private String lname, fname, dob, phone, place, info;
public Person(String lname, String fname, String dob, String phone, String place, String info){
this.lname = lname;
this.fname = fname;
this.dob = dob;
this.phone = phone;
this.place = place;
this.info = info;
}
public String getLname(){
return lname;
}
public String getFname(){
return fname;
}
public String getDob(){
return dob;
}
public String getPhone(){
return phone;
}
public String getPlace(){
return place;
}
public String getInfo(){
return info;
}
public String toOutputFormat(){
return this.fname + this.lname + this.dob + this.phone + this.place + this.info;
}
}
I already have a String in 'Info', but I would like to add new info to 'Info' (not replace the old info), so when I go to check all info about that person ther will be
"Old" info: blah blah blah
"New" info: (if I could get the date to stick here too it would be awsome) Jada jada jada
I have tried to google this but with no luck. I'm not even sure you can do this... But I'm sure that if you can, this is the place to look :)
Thank you for your time
Upvotes: 1
Views: 101
Reputation: 113
It's a little difficult to understand what you want to do - but I guess you want to modify the info String in the Person class - this can be achieved by adding a setter:
public setInfo(String newInfo){
info = newInfo;
}
or even adding:
public addInfo(String newInfo){
info += newInfo;
}
using it looks like:
EDIT:
import java.util.*;
public class Head {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String lname, fname, y, m, d, x, dob, phone, place = null, info;
int choice;
ArrayList<Person>plist = new ArrayList<>();
ArrayList<Faktura>flist = new ArrayList<>();
Person p1 = new Person("Shaerer", "Lucas", "200307302232", "0702-523219", "Stockholm", "Her we put some info");
Person p2 = new Person("Smith", "Jacob", "197609071464", "0761-617163", "Miami", "Some information about this person");
plist.add(p1);
plist.add(p2);
p1.setInfo("SomeThing new and exciting");
p1.addInfo(" and exhilarating");
System.out.println(p1.getInfo());
}
Since I don't know where you're going with this - I'm just providing an example. Above code will add your two persons to plist and afterwards edit their info field.
Upvotes: 1