1

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

1
  • Can you please create am MCVE Commented Nov 16, 2015 at 20:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much. I'm sorry I didn't explain so well. The user will be able to use the already programmed persons or add new ones. When you add a person, you will add name, date of birth, info and more. The 'info' will be a string that says something about the person. The user should also be able to add new info to that specific person. Think like a medical journal or a company that keeps track on phonecalls and what they are about.
Well, does the above answer your question then? You might want to use an ArrayList<String> info; instead of just a string, to be able to distinguish different entries...
I am working on what you gave me, haha. I'm a newbee at this, so I don't work very fast, I have to figure out how and where to put the things you gave me. The idea with ArrayList<String> info sounds very good tho. Then I just gotta figure out how to do that. Haha, it never ends, right. Thanks again for your time and help. Much appreciated :)
You're welcome! If/When you decide that the answer fits your need, please accept the answer (Or just keep asking ;))
Ok.... I don't wanna be rude or unthankfull, but if you have the time and will, maybe you could explain to me how to use the information you gave me. If it's ok. Thanks again :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.