Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将userinput添加到ArrayList java_Java_Arraylist_Add - Fatal编程技术网

将userinput添加到ArrayList java

将userinput添加到ArrayList java,java,arraylist,add,Java,Arraylist,Add,我被这件事缠住了。我正在尝试向ArrayList中的“旧”信息添加新信息。我尝试过几种不同的方法,但我无法让它停留在那里 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,

我被这件事缠住了。我正在尝试向ArrayList中的“旧”信息添加新信息。我尝试过几种不同的方法,但我无法让它停留在那里

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;
我在'Info'中已经有一个字符串,但我想在'Info'中添加新信息(而不是替换旧信息),所以当我去检查关于那个人的所有信息时,就会有新信息

“旧”信息: 废话废话

“新”信息:(如果我能把日期也贴在这里,那就太糟糕了) 加达加达加达

我试过用谷歌搜索这个,但没有成功。我甚至不确定你能做到这一点。。。但我相信,如果可以的话,这是一个值得一看的地方:)


感谢您抽出时间

理解您想做什么有点困难-但我想您想修改Person类中的信息字符串-这可以通过添加setter来实现:

public setInfo(String newInfo){
    info = newInfo;
}
甚至添加:

public addInfo(String newInfo){
    info += newInfo;
}
使用它看起来像:

编辑:

import java.util.*;
公共班主任{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
字符串lname,fname,y,m,d,x,dob,phone,place=null,info;
智力选择;
ArrayListList=新的ArrayList();
ArrayListflist=新的ArrayList();
人员p1=新人(“谢勒”、“卢卡斯”、“20030730232”、“0702-523219”、“斯德哥尔摩”、“她我们放了一些信息”);
人员p2=新人(“史密斯”、“雅各布”、“197609071464”、“0761-617163”、“迈阿密”、“关于此人的一些信息”);
plist.add(p1);
plist.add(p2);
p1.setInfo(“新的和令人兴奋的东西”);
p1.addInfo(“令人振奋”);
System.out.println(p1.getInfo());
}
因为我不知道你要说什么-我只是举个例子。
上面的代码将把你的两个人添加到plist,然后编辑他们的信息字段。

你能创建吗?非常感谢。很抱歉我没有解释清楚。用户将能够使用已经编程的人或添加新的人。当你添加一个人时,你将添加姓名、出生日期、信息等。“信息”将是一个字符串t表示有关此人的一些信息。用户还应该能够向特定的此人添加新信息。就像医学杂志或跟踪电话呼叫及其内容的公司一样思考。那么,上述问题是否回答了您的问题?您可能希望使用ArrayList信息,而不仅仅是字符串,以便能够区分不同的内容ent条目…我正在研究你给我的东西,哈哈。我在这方面是新手,所以我的工作速度不是很快,我必须弄清楚如何以及在哪里放置你给我的东西。ArrayList信息的想法听起来很好。然后我只需要弄清楚如何做。哈哈,这永远不会结束,对吧。再次感谢你的时间和帮助。非常感谢:)不客气!如果/当你决定答案符合你的需要时,请接受答案(或继续提问)好吧……我不想粗鲁或不感激,但如果你有时间和意愿,也许你可以向我解释如何使用你给我的信息。如果可以的话。再次感谢:)
public addInfo(String newInfo){
    info += newInfo;
}
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());
    }