【发布时间】: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
-
下次不用等了。您可以(并且应该)编辑您的当前问题。