【问题标题】:How do I store an array of records into each field without having to define the array position?如何在不必定义数组位置的情况下将记录数组存储到每个字段中?
【发布时间】:2017-08-06 07:22:46
【问题描述】:

我试图提示用户输入信息并将其写入文本文件,然后程序从文本文件中读取,并且有一个计数器确定数组记录的长度。但我目前被困在如何在不定义数组位置的情况下将每个字段存储到记录数组中。到目前为止,它成功地从文本文件中读取,但是当我写入该文本文件时,会出现空指针异常错误。

这是我的代码的 sn-p

static class publisherDetails
{
    String pubID;
    String publisher;
    String pubAddress;
}

public static void publisherDetails() throws IOException
{
    File publisherMain = new File("publisherMain.txt");
    Scanner readpublisher = new Scanner(publisherMain);
    publisherDetails [] publisherList = new 
    publisherDetails[countPubLines(0, publisherMain)];
    createPublisher(publisherList, publisherMain, readpublisher);
    readpublisher.close();
    printPublisher(publisherList);
}

public static int countPubLines(int count, File publisherMain) throws IOException
{
  Scanner countingLines = new Scanner(publisherMain);

  while (countingLines.hasNextLine())
  {
     String reading = countingLines.nextLine();
     count++;
  } 

  countingLines.close();
  count = count/3;
  return count;
}

public static void createPublisher(publisherDetails[] publisherList, File publisherMain, Scanner readpublisher) throws IOException
{
while ( readpublisher.hasNextLine() )
    {
       for (int i=0; i< publisherList.length; i++) 
       publisherList[i] = new publisherDetails();
        {
          publisherList[0].pubID = readpublisher.nextLine();
          publisherList[0].publisher =readpublisher.nextLine();
          publisherList[0].pubAddress =readpublisher.nextLine();
          publisherList[1].pubID =readpublisher.nextLine();
          publisherList[1].publisher =readpublisher.nextLine();
          publisherList[1].pubAddress =readpublisher.nextLine();
          publisherList[2].pubID =readpublisher.nextLine();
          publisherList[2].publisher =readpublisher.nextLine();
          publisherList[2].pubAddress =readpublisher.nextLine();
          publisherList[3].pubID =readpublisher.nextLine();
          publisherList[3].publisher =readpublisher.nextLine();
          publisherList[3].pubAddress =readpublisher.nextLine();
        }

    }
    readpublisher.close();
}

public static void printPublisher(publisherDetails[] publisherList)  
{
for (int j=0 ;j<publisherList.length; j++)
     {
       output("Publisher ID : " + publisherList[j].pubID);
       output("Publisher : " + publisherList[j].publisher);
       output("Publisher Address :  " + publisherList[j].pubAddress);
     }

}  


public static void addPublisher() throws IOException
{
   FileWriter inputPublisher = new FileWriter(new File("publisherMain.txt",true);
   newPublisher(inputPublisher);
}


public static void newPublisher(FileWriter inputPublisher) throws IOException
  {
    Scanner scan = new Scanner(System.in);
    output("Please enter publisher ID");
    String ID = scan.nextLine();
    inputPublisher.write("\n"+ID);

    output("Please enter Publisher ");
    String publisher = scan.nextLine();
    inputPublisher.write("\n"+publisher);

    output("Please enter publisher Address");
    String pubAddress = scan.nextLine();
    inputPublisher.write("\n"+pubAddress);

    scan.close();
    inputPublisher.close();
  }

【问题讨论】:

  • 请在格式化代码方面付出更多努力。目前真的很难阅读,到处都是缩进。我强烈怀疑您也不需要大约 100 行代码来演示该问题 - 请将其减少为 minimal reproducible example
  • (我强烈建议您也开始更严格地遵循 Java 命名约定 - publisherList 不是传统的类名。)
  • @JonSkeet 指出,谢谢!我对此很陌生,所以请原谅我,下次我一定会记住这一点! :D
  • 下次不用等了。您可以(并且应该)编辑您的当前问题。

标签: java arrays records


【解决方案1】:

您不能只使用ArrayList 有什么具体原因吗?一次读取整个文件似乎很浪费,只是为了确定数组的大小,然后再次读取它以获得记录的实际值。

【讨论】:

  • 嗯,没有任何理由,但我对 Java 还是很陌生,我并没有意识到这一点。这是我目前正在学习的软件课程的一个项目,我在 Java 方面的知识非常有限,但是如果您能详细说明我如何使用 ArrayList,那会很有帮助?谢谢! :D
  • 查看 Oguz 的答案以获取示例。 Java 中的ArrayList 基本上是一个动态数组。您可以向其中添加新元素,它会自动调整大小,因此您在创建它时不需要知道元素的数量。可以在 API 中找到相应的方法。可以使用增强的 for 循环对其进行迭代。
【解决方案2】:

如果您要使用关键字搜索数据,那么我认为您应该使用地图。您必须更改“createPublisher”和“printPublisher”方法。

public static Map<String, publisherDetails> createPublisher(File publisherMain, Scanner readpublisher)
        throws IOException {

    Map<String, publisherDetails> publisherMap = new HashMap<String, publisherDetails>();
    publisherDetails newPublisher = new publisherDetails();
    while (readpublisher.hasNextLine()) {

        newPublisher.pubID = readpublisher.nextLine();
        newPublisher.publisher = readpublisher.nextLine();
        newPublisher.pubAddress = readpublisher.nextLine();
        // Fill the map 
        publisherMap.put(newPublisher.pubID, newPublisher);
        readpublisher.nextLine();
    }
    readpublisher.close();
    return publisherMap;
}

public static void printPublisher(Map<String, publisherDetails> publisherMap) {
    // loop through the whole data
    // or you can search with key like publisherMap.get(pubId); 
    for(Map.Entry<String,publisherDetails> entry : publisherMap.entrySet()){

        output("Publisher ID : "       + entry.getValue().pubID);
        output("Publisher : "          + entry.getValue().publisher);
        output("Publisher Address :  " + entry.getValue().pubAddress);
    }

}

当然,您也可以使用 ArrayList。方法如下。

public static List<publisherDetails> createPublisher(File publisherMain, Scanner readpublisher)
        throws IOException {

    List<publisherDetails> publisherList = new ArrayList<publisherDetails>();
    publisherDetails newPublisher = new publisherDetails();
    while (readpublisher.hasNextLine()) {
        readpublisher.nextLine();
        newPublisher.pubID = readpublisher.nextLine();
        newPublisher.publisher = readpublisher.nextLine();
        newPublisher.pubAddress = readpublisher.nextLine();
        // Fill the list
        publisherList.add(newPublisher);

    }
    readpublisher.close();
    return publisherList;
}

public static void printPublisher(List<publisherDetails> publisherList) {
    // loop through the whole data
    for(publisherDetails publisher : publisherList){

        output("Publisher ID : "       + publisher.pubID);
        output("Publisher : "          + publisher.publisher);
        output("Publisher Address :  " + publisher.pubAddress);
    }

}

【讨论】:

  • 一般来说,但尤其是在使用集合时,您应该始终在返回值和参数中使用接口而不是实现类型。所以printPublisher() 应该带一个List 参数,createPublisher() 应该返回一个List
  • 我已经尝试了代码,它运行良好,但是当我尝试将新信息写入文件时,它只显示新信息而不是以前的信息。
  • 您的意思是向 publisherDetails 类添加新属性吗?首先,您应该在 publisherDetails 类中添加新属性,然后确保 readpublisher 具有新功能。此外,您可以检查输出方法。
  • 我的意思是它读取文本文件并且有 4 个不同的 PubID,(其他详细信息),但它使用 txt 文件中的相同数据(最后一个数据)打印所有三个字段 4 次
  • 哇,我明白了。您应该添加 readpublisher.nextLine();在while循环中。 :)
猜你喜欢
  • 2017-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多