Java 存储输入的详细信息,并显示每个新输入的所有详细信息。(爪哇)

Java 存储输入的详细信息,并显示每个新输入的所有详细信息。(爪哇),java,arrays,java.util.scanner,delimiter,Java,Arrays,Java.util.scanner,Delimiter,我不知道如何处理这段代码。我试图提示用户输入一个人的详细信息(如姓名、年龄和电话号码),然后输出所有个人信息。无论何时输入另一个人的信息,都会输出前一个人的信息以及当前一个人的信息 应该是这样的: private static void processRecords() throws FileNotFoundException { List<List<Object>> recordList = new ArrayList<List<Object>&g

我不知道如何处理这段代码。我试图提示用户输入一个人的详细信息(如姓名、年龄和电话号码),然后输出所有个人信息。无论何时输入另一个人的信息,都会输出前一个人的信息以及当前一个人的信息

应该是这样的:

private static void processRecords() throws FileNotFoundException
{
  List<List<Object>> recordList = new ArrayList<List<Object>>();
  // scan data line by line
  Scanner scanner = new Scanner(new File("/tmp/foo.txt"));

  while(scanner.hasNextLine())
  {
     List<Object> record = new ArrayList<Object>();
     String line = scanner.nextLine();


     //remove all spaces, since nextDouble and nextInt
     //will choke on them:
     line = line.replaceAll(" ", "");

     //create a scanner just for this line
     Scanner recordScanner = new Scanner(line);
     recordScanner.useDelimiter(",");
     String sData1 = recordScanner.next();
     double sData2 = recordScanner.nextDouble();
     double sData3 = recordScanner.nextDouble();
     String sData4 = recordScanner.next();

     record.add(sData1);
     record.add(sData2);
     record.add(sData3);
     record.add(sData4);
     // add the record to the list.
     recordList.add(record);

     //print it out
     printHeader(recordList);

  }
}

private static void printHeader(List<List<Object>> recordList)
{
  //print it out
  for (List<Object> recordToPrint : recordList)
  {
     for (Object objToPrint : recordToPrint)
     {
        //print your records.
        System.out.print(objToPrint);
        System.out.print(",");
     }


     System.out.println();
  }
}
我的输入:亚当,2512345678,法国

输出:Adam,2512345678,法国

我的第二次输入:鲍勃,22,12345678,澳大利亚

第二次输出:

               Adam, 25, 12345678, France             
               Bob, 22, 12345678, Australia
以此类推,10次将创建第十次包含所有细节的表格。 关于我如何做整个工作,有什么想法吗?每次都创建一个包含更多细节的表格?我尝试过用它来存储变量,但循环数组可能更好?我是Java新手,因为我最近才开始学习。任何例子都将不胜感激。非常感谢

    Scanner scan = new Scanner(System.in);
    scan.useDelimiter(",");     

    String sName = scan.next();
    System.out.println(header);
    String sLast = scan.next();
    double sData1 = scan.nextDouble();
    double sData2 = scan.nextDouble();
    double sData3 = scan.nextDouble();
    int sData4 = scan.nextInt();

当我回到我的电脑上时,我会完全检查回来。感谢您的快速响应。

您可以使用两个扫描仪,一个用于分隔行,另一个用于用逗号拆分,然后将正在扫描的数据构建为列表列表,然后迭代打印出来。大概是这样的:

private static void processRecords() throws FileNotFoundException
{
  List<List<Object>> recordList = new ArrayList<List<Object>>();
  // scan data line by line
  Scanner scanner = new Scanner(new File("/tmp/foo.txt"));

  while(scanner.hasNextLine())
  {
     List<Object> record = new ArrayList<Object>();
     String line = scanner.nextLine();


     //remove all spaces, since nextDouble and nextInt
     //will choke on them:
     line = line.replaceAll(" ", "");

     //create a scanner just for this line
     Scanner recordScanner = new Scanner(line);
     recordScanner.useDelimiter(",");
     String sData1 = recordScanner.next();
     double sData2 = recordScanner.nextDouble();
     double sData3 = recordScanner.nextDouble();
     String sData4 = recordScanner.next();

     record.add(sData1);
     record.add(sData2);
     record.add(sData3);
     record.add(sData4);
     // add the record to the list.
     recordList.add(record);

     //print it out
     printHeader(recordList);

  }
}

private static void printHeader(List<List<Object>> recordList)
{
  //print it out
  for (List<Object> recordToPrint : recordList)
  {
     for (Object objToPrint : recordToPrint)
     {
        //print your records.
        System.out.print(objToPrint);
        System.out.print(",");
     }


     System.out.println();
  }
}
private static void processRecords()引发FileNotFoundException
{
List recordList=new ArrayList();
//逐行扫描数据
Scanner Scanner=new Scanner(新文件(“/tmp/foo.txt”);
while(scanner.hasNextLine())
{
列表记录=新的ArrayList();
字符串行=scanner.nextLine();
//删除所有空格,因为nextDouble和nextInt
//会让他们窒息:
line=line.replaceAll(“,”);
//仅为此行创建一个扫描仪
扫描仪记录扫描仪=新扫描仪(行);
recordScanner.useDelimiter(“,”);
字符串sData1=recordScanner.next();
double sData2=recordScanner.nextDouble();
double sData3=recordScanner.nextDouble();
字符串sData4=recordScanner.next();
记录。添加(sData1);
记录。添加(sData2);
记录。添加(sData3);
记录。添加(sData4);
//将记录添加到列表中。
记录列表。添加(记录);
//打印出来
打印头(记录列表);
}
}
私有静态void打印头(列表记录列表)
{
//打印出来
用于(列表记录打印:记录列表)
{
用于(对象对象对象对象打印:记录打印)
{
//打印你的记录。
系统输出打印(objToPrint);
系统输出打印(“,”);
}
System.out.println();
}
}
注意:我已更改代码以匹配您的数据-您提供的代码示例与您提供的数据不太匹配。

您可以使用scan.nextLine()获取每个人的信息,然后使用列表存储所有人的信息。最后,打印列表中存储的所有人员信息

这样,您可以一次输入一个人的信息,例如,在控制台中输入Adam、2512345678、France

以下是一个例子:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    List<String> personInformation = new ArrayList<String>();

    String line = null;

    while (!"exit".equals(line = scan.nextLine())) {
        personInformation.add(line);
        printPersonInformation(personInformation);
    }

}

private static void printPersonInformation(List<String> personInformation) {
    System.out
            .println("All the person information we have now is as follows:");
    for (String personInfo : personInformation) {
        System.out.println(personInfo);
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
List personInformation=新建ArrayList();
字符串行=null;
而(!“exit”.equals(line=scan.nextLine()){
人员信息。添加(行);
打印个人信息(个人信息);
}
}
私有静态无效打印个人信息(列表个人信息){
系统输出
.println(“我们现在掌握的所有人员信息如下:”);
for(字符串personInfo:personInformation){
System.out.println(personInfo);
}
}

我认为最好的方法之一是创建一个person类,然后将每个人添加到列表中,在每个条目后打印整个列表。您会发现创建Person类并为其实现toString()方法最简单:

public class TestCode {
    public static void main (String args []) {
        Scanner input = new Scanner(System.in);

        List<Person> peopleList = new ArrayList<Person>();

        for(int i = 0; i < 10; i++){
            System.out.println("enter info: ");
            String name = input.next();
            String age = input.next();
            String phone = input.next();
            String location = input.next();
            peopleList.add(new Person(name, Integer.parseInt(age), Integer.parseInt(phone), location));

            for(Person p: peopleList)
                 System.out.println(p.toString());
        }
    }
}

class Person{
    String name;
    int age;
    int phoneNumber;
    String location;

    Person(String n, int a, int p, String l){
        this.name = n;
        this.age = a;
        this.phoneNumber = p;
        this.location = l;
    }

    public String toString(){
        return(name + " " + age + " " + phoneNumber + " " + location);
    }
}
公共类测试代码{
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
List peopleList=新的ArrayList();
对于(int i=0;i<10;i++){
System.out.println(“输入信息:”);
String name=input.next();
String age=input.next();
字符串phone=input.next();
字符串位置=input.next();
添加(新的人物(姓名,Integer.parseInt(年龄),Integer.parseInt(电话),位置));
对于(人员p:人员列表)
System.out.println(p.toString());
}
}
}
班主任{
字符串名;
智力年龄;
整数音素;
字符串位置;
Person(字符串n、整数a、整数p、字符串l){
this.name=n;
这个年龄=a;
this.phoneNumber=p;
这个位置=l;
}
公共字符串toString(){
返回(姓名+年龄+电话号码+位置);
}
}

类似于此,尽管此示例不包括从输入中删除逗号。如果选择此方法,则在将值存储到People类之前,应先处理从输入中删除逗号的问题。

步骤1。创建一个类来保存有关个人的数据,包括最终字段、合适的构造函数和
toString()
方法:

public class Person {
    final String name, country;
    final int age, phone;
    public Person(String name, int age, int phone, String country) {
        this.name = name;
        this.age = age;
        this.phone = phone;
        this.country = country;
    }
    public String toString() {
        return name + ", " + age + ", " + phone + ", " + country;
    }
    // getters omitted for brevity
}
步骤2:创建以下内容的列表:

List<Person> people = new ArrayList<Person>();
准备打印时:

for (Person person : people) {
    System.out.println(person);
}

你能粘贴输出它的代码吗?当你使用nextDouble()或nextInt()时,“\n”不会被读取…而且如果你在这里放入更多的代码,那么确切的错误可能会被pin指向..你需要一个ArrayList来存储已经输入的用户信息这基本上就是我的建议。为什么要使用最终的类变量?不。可能最大的区别是使用列表来存储“person”,这是一种糟糕的设计:它完全没有类型,很难使用和调试。任何使用列表>的问题的答案都是错误的答案,除非是在最理性的情况下(我记不起这种情况)。为什么是决赛?因为它是