홈>
사용자에게 정보를 묻는 메시지를 표시하고이를 텍스트 파일에 쓰려고하면 프로그램이 텍스트 파일에서 읽고 배열 레코드의 길이를 결정하는 카운터가 있습니다. 그러나 현재 배열 위치를 정의하지 않고 각 필드를 레코드 배열에 저장할 수있는 방법에 붙어 있습니다. 지금까지 텍스트 파일을 읽는 데 성공했지만 해당 텍스트 파일에 쓸 때 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); } }
관련 자료
- ios - coredata - 변형 가능한 개체 배열 저장
- php - 루프없이 배열의 줄 바꿈 바꾸기
- 배열 키없이 Swift에서 JSON 구문 분석
- javascript - 배열의 배열을 중복없이 배열로 변환
- node.js - 동일한 배열 필드에 많은 업데이트 연산자 적용
- javascript - 키에 at 기호 (@)없이이 객체 배열을 유지하는 방법 - es2006/es2020
- javascript - 마지막 항목을 전달할 때 첫 번째 항목에 대한 자동 루프를 사용하여 배열에 마지막 N 값을 저장하는 방법은 무엇입니까?
- json - 어레이 필드 (AWS Glue)를 쿼리하는 방법은 무엇입니까?
- javascript - 반응 후크 형식 - 대화 상자 내의 필드 배열 (머티리얼 ui)
- eval 함수를 사용하지 않고 파이썬으로 파일의 특정 줄에서 json 배열을 읽는 방법
- javascript - 텍스트 파일에서 데이터를 읽고 객체 배열에 저장합니다NodeJS
- 페이팔 API에서 복잡한 json 반환 배열의 PHP 변수 저장
- javascript - HTML 컬렉션을 비우지 않고 배열로 변환하려면 어떻게합니까? # 2
- c - uint64_t를 배열에 어떻게 저장할 수 있습니까?
- ansi c - 텍스트 파일 열의 데이터를 배열에 저장합니다 C에서
- angular - 객체에서 배열로 속성 저장
- 배열 생성없이 C # 문자열 분할
- 홈 디렉토리없이 SSH 키를 어떻게 저장할 수 있습니까?
- c - 크기를 모르고 동적 배열을 반복 할 수 있습니까?
- C의 배열에 행렬을 어떻게 저장합니까?
관련 질문
- Java 배열 값을 덮어 씁니다.
- java : 2 차원 배열의 자연 결합
- 여러 형식의 텍스트 파일을 분할하여 객체 Java 생성
- java : 공백으로 들여 쓴 사용자 입력을 String 배열에 저장하고 숫자를 int 배열로 변환하려면 어떻게해야합니까?
- java : 대각 계수를 고려하지 않고 행렬 행의 최대 값을 반환합니다.
- java : 배열을 자동으로 만드는 방법
- 무게 중심과 측면의 길이가 있다는 것을 알고있는 정사각형의 정점을 Java에서 어떻게 코딩해야합니까?
- Java : int []를 범위로 가장 작은 표현으로 변환
- java : 선행 0이 아닌 int 배열 만 복사하는 방법
- java : 이미 채워진 정수 배열 목록에 값을 추가 하시겠습니까?
와이즈 비를 사용할 수없는 특별한 이유가 있습니까? ? 전체 파일을 한 번 읽고 배열의 크기를 확인한 다음 다시 읽어서 레코드의 실제 값을 가져 오는 것은 꽤 낭비입니다.