2017-08-06 57 views
2

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

这是我的代码

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

请花更多的精力来设置代码的格式。目前*真的很难阅读,并且到处都有缩进。我强烈怀疑你并不需要~100行代码来证明这个问题,请将它降低到[mcve]。 –

+0

(我强烈建议你更仔细地遵循Java命名约定 - 'publisherList'不是传统的类名。) –

+0

@JonSkeet指出,谢谢!我很新,所以请原谅我,我一定会在下一次考虑到这一点! :D – lyrehcx

回答

1

一个片段是有,为什么你不能只用一个ArrayList具体原因是什么?读取整个文件一次看起来非常浪费,只是为了确定数组的大小,然后再次读取它以获取记录的实际值。

+0

嗯,没有理由,但我对Java很新,我不知道它。这是一个我正在使用的软件类的项目,我在java中的知识是相当有限的,但是如果你能详细说明如何使用ArrayList,这会有所帮助。谢谢! :D – lyrehcx

+0

有关示例,请参阅Oguz的答案。 Java中的ArrayList基本上是一个动态数组。您可以添加新元素并自动调整大小,因此在创建元素时不需要知道元素的数量。相应的方法可以在API中找到。它可以通过使用增强型for循环来迭代。 –

0

如果您要通过数据搜索关键字,那么我认为您应该使用地图。您必须更改“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); 
    } 

} 
+1

通常,但特别是在使用集合时,应该始终使用接口而不是返回值和参数中的实现类型。所以'printPublisher()'应该带一个'List'参数,'createPublisher()'应该返回一个'List'。 –

+0

你说得对。我编辑它。 – Oguz

+0

我试过了代码,它的工作原理非常完美,但是当我尝试将新信息写入文件时,它只显示新信息而不显示以前的信息。 – lyrehcx

相关问题