如何在不必定义数组位置的情况下将记录数组存储到每个字段中?

时间:2017-08-06 07:22:46

标签: java arrays records

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

这是我的代码片段

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

2 个答案:

答案 0 :(得分:1)

您是否有一个特定的理由不能使用ArrayList?读取整个文件一次似乎相当浪费,只是为了确定数组的大小,然后再次读取它以获得记录的实际值。

答案 1 :(得分:0)

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

RunInstances

或者您当然可以使用ArrayList。这是如何。

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

}