如何使用Java查找、读取和显示文本文件中的行
我有一个包含假飞机记录的文本文件。我需要编写一个代码,根据记录的ID字符串查找记录并显示它。我有一个主文件、类文件和文本文件。基本上,用户应该能够输入“ID”,比如“DA0”,代码将找到它,并显示该行
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lastassignment;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author djiml
*/
public class LastAssignment {
public LastAssignment() {
}
private ArrayList<AirplaneSeatRecords> airplaneSeats = new ArrayList<AirplaneSeatRecords>();
Scanner scnr = new Scanner(System.in);
String airlineIndex;
String name;
int seat;
double ticketCost;
public void OpenRecord() throws IOException {
FileInputStream inputfile = new FileInputStream("input.txt");
Scanner inFile = new Scanner(inputfile);
while (inFile.hasNext()) {
airlineIndex = inFile.next();
ticketCost = inFile.nextDouble();
seat = inFile.nextInt();
name = inFile.nextLine();
airplaneSeats.add(new AirplaneSeatRecords(airlineIndex, ticketCost, seat, name));
}
inFile.close();
}
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
int input;
System.out.println("What would you like to do?:");
System.out.println("1. Open a Record.");
System.out.println("2. Display a Record.");
System.out.println("3. Display All Records.");
System.out.println("4. Add a New Record.");
System.out.println("5. Edit a Record.");
System.out.println("6. Delete a Record.");
System.out.println("7. Save Progress.");
System.out.println("8. Exit.");
System.out.print(": ");
input = scnr.nextInt();
if (input != 8) {
switch(input) {
case 1 -> new LastAssignment().OpenRecord();
case 2 -> System.out.println("FIXME: Add 'Display a Record'");
case 3 -> System.out.println("FIXME: Add 'Display All Records'");
case 4 -> System.out.println("FIXME: Add 'Add a New Record'");
case 5 -> System.out.println("FIXME: Add 'Edit a Record'");
case 6 -> System.out.println("FIXME: Add 'Delete a Record'");
case 7 -> System.out.println("FIXME: Add 'Save Progress'");
}
}
else {
System.out.println("FIXME: Add 'Exit'");
}
}
}
类文件
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lastassignment;
/**
*
* @author djiml
*/
public class AirplaneSeatRecords {
private String airlineIndex;
private double ticketCost;
private String customerFlying;
private int seatNum;
public AirplaneSeatRecords() {
// Constructor
}
public AirplaneSeatRecords(String ID, double cost, int seat, String name) {
airlineIndex = ID;
ticketCost = cost;
seatNum = seat;
customerFlying = name;
}
public void displayTickets() {
System.out.printf("%8s \t %-20s \t\t $%.2f \t %d \n", airlineIndex, customerFlying, ticketCost, seatNum);
}
public String getTicketID() {
return airlineIndex;
}
public double getTicketCost() {
return ticketCost;
}
public int getSeatNum() {
return seatNum;
}
public String getCustomerName() {
return customerFlying;
}
public void setSeatNum(int seat) {
seatNum = seat;
}
public void setCustomerName(String name) {
customerFlying = name;
}
public void setTicketPrice(double price) {
ticketCost = price;
}
}
文本文件
DA0 215.00 19 Doug Anderson
DA1 200.00 20 Drew Bates
DA2 185.00 21 Theresa Patty
DA3 170.00 22 Andrew Abrams
# 1 楼答案
在
case 2
中,再次向用户索要ID,并用扫描仪读取创建一个方法,在其中通过
airplaneSeats
数组在循环中进行迭代,如果输入的ID与数组中AirplaneSeatRecords
之一的ID匹配,则打印记录并中断循环。在AirplaneSeatRecords
类中已经有了必要的方法另外,不要忘记在switch语句中添加一个默认情况