J'essaie d'inviter l'utilisateur pour l'information et l'écrire dans un fichier texte, puis le programme lit à partir du fichier texte et il y a un compteur qui détermine la longueur de l'enregistrement du tableau. Mais je suis actuellement bloqué sur la façon dont je peux stocker chaque champ dans un tableau d'enregistrement sans définir la position du tableau. Jusqu'à présent, il réussit à lire à partir du fichier texte, mais quand j'écris dans ce fichier texte, il y aura une erreur d'erreur de pointeur nul.Comment stocker un tableau d'enregistrements dans chaque champ sans avoir à définir la position du tableau?
C'est un extrait de mon code
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();
}
Veuillez mettre plus d'effort dans le formatage de votre code. C'est vraiment difficile à lire en ce moment, avec indentation partout. Je soupçonne fortement que vous n'avez pas réellement besoin de ~ 100 lignes de code pour démontrer le problème, merci de le réduire à un [mcve]. –
(Je vous recommande fortement de suivre de plus près les conventions de nommage Java - 'publisherList' n'est pas un nom de classe conventionnel.) –
@JonSkeet noté, merci! Je suis assez nouveau à ce sujet alors s'il vous plaît pardonnez-moi, je vais être sûr de garder cela à l'esprit la prochaine fois! : D – lyrehcx