>

사용자에게 정보를 묻는 메시지를 표시하고이를 텍스트 파일에 쓰려고하면 프로그램이 텍스트 파일에서 읽고 배열 레코드의 길이를 결정하는 카운터가 있습니다. 그러나 현재 배열 위치를 정의하지 않고 각 필드를 레코드 배열에 저장할 수있는 방법에 붙어 있습니다. 지금까지 텍스트 파일을 읽는 데 성공했지만 해당 텍스트 파일에 쓸 때 null 포인터 예외 오류가 발생합니다.

이 코드는 스 니펫입니다

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

    와이즈 비를 사용할 수없는 특별한 이유가 있습니까? ? 전체 파일을 한 번 읽고 배열의 크기를 확인한 다음 다시 읽어서 레코드의 실제 값을 가져 오는 것은 꽤 낭비입니다.

  • 답변 # 2

    데이터를 확인한 다음지도를 사용해야한다고 생각합니다. "createPublisher"및 "printPublisher"메소드를 변경해야합니다.

    ArrayList
    
    

    또는 물론 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);
        }
    }
    
    
    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); } }

관련 자료

  • 이전 xaml - UWP 커뮤니티 툴킷 RoundImageEx 이미지가 표시되지 않음
  • 다음 Windows 2012에 우분투 bash를 설치하는 방법?