所以基本上我需要做的是让这个while循环遍历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文件
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格式
这是我为之前的作业编写的类似方法
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 :(得分:0)
我会提供一些提示来帮助你:
1)你知道如何逐行阅读。您可以选择何时开始,停止甚至跳过线。可悲的是,你只能前进,但这足以解决这个问题。
2)数据文件遵循恒定模式。这意味着这是易于处理的数据。
3)你事先知道这个模式。 Pst,这是OP!
我认为就是这样。现在你应该注意并等待AHA!时刻。
答案 1 :(得分: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;
}
}