에 할당 그래서 기본적으로 내가배열에 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
무엇이 당신 질문입니까? 너의 문제는 무엇인가? 너 뭐 해봤 니? Stackoverflow는 "나에게 이런 짓을하다"사이트로 설계되지 않았기 때문에 이미 조사한 주제에 대해 질문을하기 위해 여기에옵니다. –
내 질문에 대한 어떤 종류의 힌트 또는 방향을 여기에 대한 루프 및 비슷한 방법을 사용할 수있는 시도 했으므로 여기에 할 –
input.hasNextLine() input.hasNext() 변경 한 줄씩 줄 잡기 때문에 with input.nextLine() –