简体   繁体   English

如何使用 Java 从文本文件中查找、读取和显示一行

[英]How to find, read and display a line from a text file with Java

I have a text file entailing fake airplane records.我有一个包含假飞机记录的文本文件。 I need to write a code that will find a record based on its ID String and display it.我需要编写一个代码,该代码将根据其 ID 字符串查找记录并显示它。 I have a main, class, and text file.我有一个主要的 class 和文本文件。 Basically the user should be able to enter the "ID" say 'DA0' and the code will find it, and display the line.基本上用户应该能够输入“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'");
        }
    }
    
}

Class File Class 文件

/*
 * 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;
    }
    
    
}

Text File文本文件

DA0 215.00 19 Doug Anderson
DA1 200.00 20 Drew Bates
DA2 185.00 21 Theresa Patty
DA3 170.00 22 Andrew Abrams

In case 2 ask the user again for an ID and read it with Scanner. case 2再次向用户询问 ID 并使用 Scanner 读取。

Create a method where you iterate in a loop through your airplaneSeats array, and if the entered ID matches the ID of one of the AirplaneSeatRecords in your array, then print the record and break the loop.创建一个方法,在其中循环遍历您的airplaneSeats数组,如果输入的 ID 与数组中的一个AirplaneSeatRecords的 ID 匹配,则打印记录并中断循环。 You already have the necessary methods for it in the AirplaneSeatRecords class.您已经在AirplaneSeatRecords class 中有必要的方法。

Also, don't forget to add a default case to your switch statement.另外,不要忘记在 switch 语句中添加默认情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM