2017-12-05 5 views
-1

에 할당 그래서 기본적으로 내가배열에 TXT 파일을 읽어 변수

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
/** 
* <insert class description here> 
* 
* @author Chris Crosby 
* 
*/ 
public class TrackInput 
{ 
    private Scanner keyboard = new Scanner(System.in); 

    public int readTrackData(Railroad[] Reservations) { 
     final String FILE_NAME = "TrackData.txt"; 
     int size =0; 
     Scanner input = null; 
     try { 
      input= new Scanner(new File(FILE_NAME)); 
     } 
     catch(FileNotFoundException e) { 
      System.out.println("Unable to open file " + FILE_NAME + "."); 
     } 
     String passengerName=""; 
     String routeNumber=""; 
     String departureDate=""; 
     String departureTrack=""; 
     String arrivalTrack=""; 
     String departureTime=""; 
     String arrivalTime=""; 
     String seat=""; 
     String returnRouteNumber=""; 
     String ReturnDate=""; 


     while (input.hasNext()&& size<Reservations.length) { 

     } 
     return size; 
    } 
} 

가 여기에 인스턴스 변수 안에 저장되는 txt 파일을 통해이 while 루프를 실행하고 배열에 저장되어 무엇을해야하는지 루프가

Carl Foreman 
1234 
UA1235 
06/23/2014 
ORD 
LGA 
4:00 PM 
7:15 PM 
23A 
UA673 
07/12/2014 
LGA 
ORD 
10:00 AM 
11:25 AM 
8A 
Jennifer Foreman 
1235 
UA1235 
06/23/2014 
ORD 
LGA 
4:00 PM 
7:15 PM 
23B 
UA673 
07/12/2014 
LGA 
ORD 
10:00 AM 
11:25 AM 
8B 
Jane Anderson 
4577 
UA317 
08/04/2014 
ORD 
SFO 
8:10 AM 
10:45 AM 
11C 
UA728 
08/14/2014 
SFO 
ORD 
12:52 PM 
7:03 PM 
10D 
Jason Anderson 
4578 

TrackData.txt 형식을 통해 읽고있다 txt 파일

  • 승객 이름은 - 하나의 변수
  • 예약 번호
  • 출발 노선 번호
  • 출발 날짜
  • 출발 트랙
  • 도착 트랙
  • 출발 시간
  • 도착 시간
  • 에 이름과 성을 포함
  • 좌석
  • 돌아 오는 경로 번호
  • 반환 날짜
  • 출발 트랙
  • 도착 트랙
  • 출발 시간
  • 도착 시간
  • 좌석

여기에 내가 이전에 할당

을 위해 써야했다 유사한 방법이다
public int readInventory(Automobile[] inventory) 
    { 
     final String FILENAME = "inventory.csv"; 
     int size = 0; 

     Scanner input = null; 
     try 
     { 
      input = new Scanner(new File(FILENAME)); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("Unable to open file " + FILENAME + "."); 
     } 

     // read header line and discard first line 
     String line = input.nextLine(); 

     // create vars 
     int year = 0; 
     String make = ""; 
     String model = ""; 
     double price = 0.0; 
     String condition = ""; 
     int rating = 0; 
     String status = ""; 
     String vin = ""; 

     // Customer vars 
     String firstName = ""; 
     String lastName = ""; 
     String streetAddress = ""; 
     String city = ""; 
     String state = ""; 
     String zip = ""; 
     String email = ""; 
     String phone = ""; 

     while (input.hasNext() && size < inventory.length) 
     { 
      line = input.nextLine(); 
      String[] record = line.split(","); 

      year = Integer.parseInt(record[0]); 
      make = record[1]; 
      model = record[2]; 

      // If use this version, comment out the following if statements 

      if (!(record[3].equals(""))) 
      { 
       price = Double.parseDouble(record[3]); 
      } 
      else 
      { 
       price = 0; 
      } 

      condition = record[4]; 

      if (!(record[5].equals(""))) 
      { 
       rating = Integer.parseInt(record[5]); 
      } 
      else 
      { 
       rating = 0; 
      } 

      status = record[6]; 
      vin = record[7]; 

      // this is where the records differ 
      // they either don't have buyer information or some do 

      if (record.length > 8) 
      { 
       if (!(record[8].equals(""))) 
        firstName = record[8]; 
       else 
        firstName = ""; 

       if (!(record[9].equals(""))) 
        lastName = record[9]; 
       else 
        lastName = ""; 

       if (!(record[10].equals(""))) 
        streetAddress = record[10]; 
       else 
        streetAddress = ""; 

       if (!(record[11].equals(""))) 
        city = record[11]; 
       else 
        city = ""; 

       if (!(record[12].equals(""))) 
        state = record[12]; 
       else 
        state = ""; 

       if (!(record[13].equals(""))) 
        zip = record[13]; 
       else 
        zip = ""; 

       if (!(record[14].equals(""))) 
        email = record[14]; 
       else 
        email = ""; 

       if (!(record[15].equals(""))) 
        phone = record[15]; 
       else 
        phone = ""; 

      } 

      // changes to integrate Customer class go below 

      Customer tempCustomer = new Customer(firstName,lastName, city, state, email, phone,zip,streetAddress);   
      Automobile tempAutomobile = new Automobile(year, make, model, price, 
        condition, rating, status, vin, tempCustomer); 

      inventory[size] = tempAutomobile; 
      size++; 

     } // end of while loop 

     input.close(); 

     return size; 
    } 

이 프로그램에 대해 어떻게 작동하는지 잘 모르시겠습니까? 여러 줄을 입력했기 때문에 이전 줄은 쉼표로 구분되어 있기 때문에 쉼표로 구분됩니다. line.split

+0

무엇이 당신 질문입니까? 너의 문제는 무엇인가? 너 뭐 해봤 니? Stackoverflow는 "나에게 이런 짓을하다"사이트로 설계되지 않았기 때문에 이미 조사한 주제에 대해 질문을하기 위해 여기에옵니다. –

+0

내 질문에 대한 어떤 종류의 힌트 또는 방향을 여기에 대한 루프 및 비슷한 방법을 사용할 수있는 시도 했으므로 여기에 할 –

+0

input.hasNextLine() input.hasNext() 변경 한 줄씩 줄 잡기 때문에 with input.nextLine() –

답변

0

흠 ... 나는 대답하지 않아도 될지 모르겠습니다. 그게 열심히 그리고 나는 당신이 그것을 스스로 해결할 수 있다고 확신합니다. 슬프게도 나는 단지 코멘트에 대한 충분한 명성을 얻지 못했다. (그래서 나는 처음에 여기에있다.)

난 그냥 당신을 도움이되는 몇 가지 힌트를 떨어질 것 :

1) 당신은 라인으로 라인을 읽는 방법을 알고있다. 언제 라인을 시작, 중지 및 건너 뛸 지 선택할 수 있습니다. 슬프게도 당신은 앞으로 나아갈 수 있지만,이 문제만으로 충분합니다.

2) 데이터 파일은 일정한 패턴을 따릅니다. 이는 데이터를 쉽게 처리 할 수 ​​있음을 의미합니다.

3) 미리 패턴을 알고 있습니다. Pst, 그것은 OP입니다!

나는 그렇게 생각한다. 이제는주의를 기울여 AHA를 기다려야합니다! 순간.

0
package edu.ilstu; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
/** 
* <insert class description here> 
* 
* @author Chris Crosby 
* 
*/ 
public class TrackInput 
{ 
    public final String TRANSACTION_FILE = "Transactions.txt"; 
    public final String DATA_FILE = "RailroadData.txt"; 

    public TrackInput() { 

    } 
    Scanner transaction = null; 
    public Scanner readTransactions() { 
     try { 
      transaction= new Scanner(new File(TRANSACTION_FILE)); 
     } 
     catch(FileNotFoundException e){ 
      System.out.println("Unable to open file" + TRANSACTION_FILE); 
      System.exit(1); 
     } 
     return transaction; 
    } 
    public char readTransactionCode() { 
     char choice = transaction.nextLine().charAt(0); 
     return choice; 
    } 
    public int readRailroadData(Reservation [] reservations) { 
     Scanner data; 
     int count = 0; 
     try { 
      data = new Scanner(new File(DATA_FILE)); 
      while(data.hasNext()) { 
       Reservation reservation = readReservationData(data); 
       reservations [count] = reservation; 
       count++; 
      } 
     } 
     catch(FileNotFoundException e){ 
      System.out.println("Unable to open file" + DATA_FILE); 
      System.exit(1); 
     } 
     return count; 
    } 
    public Reservation readReservationData(Scanner input) { 
     String name = input.nextLine(); 
     String departureReservationNumber=input.nextLine(); 
     String departureRouteNumber = input.nextLine(); 
     String departureRouteDate = input.nextLine(); 
     String departureTrack = input.nextLine(); 
     String departureArrivalTrack = input.nextLine(); 
     String departureTime = input.nextLine(); 
     String departureArrivalTime = input.nextLine(); 
     String departureSeatAssignment = input.nextLine(); 

     String returnRoute = input.nextLine(); 
     String returnDate = input.nextLine(); 
     String returnDepartureTrack = input.nextLine(); 
     String returnArrivalTrack = input.nextLine(); 
     String returnDepartureTime = input.nextLine(); 
     String returnArrivalTime = input.nextLine(); 
     String returnSeatAssignment = input.nextLine(); 
     Route departureRoute = new Route(departureRouteNumber, departureRouteDate, departureTrack, departureArrivalTrack,departureTime, departureArrivalTime, departureSeatAssignment); 
     Route arrivalRoute= new Route(returnRoute, returnDate, returnDepartureTrack, returnArrivalTrack,returnDepartureTime,returnArrivalTime,returnSeatAssignment); 
     Reservation reservation = new Reservation(name,departureReservationNumber, departureRoute, arrivalRoute); 
     return reservation; 
    } 
    public int addReservationToList(Reservation[] reservationList,Reservation reservation, int size) { 
     reservationList[size] = reservation; 
     size++; 
     return size; 
    } 

} 
관련 문제