【问题标题】:Storing details from an input and displaying all the details with each new input. (Java)存储来自输入的详细信息并显示每个新输入的所有详细信息。 (爪哇)
【发布时间】:2013-08-26 03:45:39
【问题描述】:

我在弄清楚如何处理这段代码时遇到了问题。我试图提示用户提供一个人的详细信息(例如姓名、年龄和电话号码),然后输出所有人员信息。每当输入另一个人的信息时,就会输出之前那个人的信息以及当前那个人的信息。

应该是这样的:

我的意见:Adam,25 岁​​,12345678,法国

输出:Adam,25 岁​​,12345678,法国

我的第二次输入:Bob,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();

当我回到我的电脑上时会全面检查。感谢您的快速回复。

【问题讨论】:

  • 你能粘贴输出它的代码吗?
  • 当你使用 nextDouble() 或 nextInt() 时,'\n' 不会被读取..如果你在这里放更多的代码,那么确切的错误可以是精确的..
  • 需要一个ArrayList来存储已经输入的用户信息

标签: java arrays java.util.scanner delimiter


【解决方案1】:

我认为解决此问题的最佳方法之一是可能创建一个人员类,然后将每个人添加到一个列表中,在每个条目之后打印整个列表。你会发现创建一个 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);
    }
}

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

【讨论】:

    【解决方案2】:

    第 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>();
    

    第 3 步:扫描 4 个单独的值,然后创建一个人,然后将其添加到列表中:

    String name = scanner.next(); // etc for the other values
    Person person = new Person(name, age, phone, country);
    people.add(person);
    

    当您准备好打印时:

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

    【讨论】:

    • 这基本上是我的建议。为什么是 final 类变量?
    • 没有。最大的区别可能是您使用 List 来存储“人”,这是一个糟糕的设计:它完全没有类型,难以使用和调试。对使用 List> 的任何问题的任何答案都是一个糟糕的答案,除非在最不合理的情况下(我不记得这种情况)。为什么是决赛?因为字段不应该改变是一种很好的做法(在这种情况下,关于一个人的这些事实是固定的)
    • 嗯,我当然不记得假装自己是这方面的专家。我建议的工作。我只是询问您对最终变量的使用,但如果您宁愿侮辱而不是提供帮助,请随意。尽管任何侮辱而不是帮助的答案也是一个糟糕的答案。
    • 我才意识到你把我误认为是别人了。我的列表不是无类型的,它与您的相同。尽管严厉的回答似乎仍然有点不具建设性。
    【解决方案3】:

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

    这样可以一次输入一个人的信息,比如在控制台输入Adam, 25, 12345678, 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);
        }
    }
    

    【讨论】:

      【解决方案4】:

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

      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();
        }
      }
      

      注意:我已更改代码以匹配您的数据 - 您提供的代码示例与您提供的数据不太匹配。

      【讨论】:

        猜你喜欢
        • 2022-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        相关资源
        最近更新 更多