繁体   English   中英

您如何允许用户从 Java 列表中检索值?

[英]How do you allow a user to retrieve values from a list in Java?

我在 FlightBookingSystem Java 类中创建了一个列表,如下所示:

public List<Flight> getFlights() {
    List<Flight> out = new ArrayList<>(flights.values());
    return Collections.unmodifiableList(out);
}

我从文本文件中导入的内容如下所示:

1::LX2500::Birmingham::Munich::2020-11-25::

2::LX2500::Denmark::London::2021-07-01::

3::LY2380::London::France::2021-06-28::

这是一个基本的文本文件,其中包含每个航班的信息

这是我要调整的代码:

public Flight execute(FlightBookingSystem flightBookingSystem, int id)
        throws FlightBookingSystemException {
    List<Flight> flights = flightBookingSystem.getFlights();
    for (Flight Flight : flights) {
        if (Flight.getFlightNumber() == flightNumber) {
            System.out.println(Flight.getFlightNumber() + " flight(s)");
            return flights.get(id);
        }
        System.out.println(((Flight) flights).getFlightNumber() + " flight(s)");

    }
    return flights.get(id);
}

如何更改该代码,以便它允许用户从文本文件中检索单个记录?

为什么不使用HashMap检索所有并通过 key 或 id 获取您想要的?

如果您仍然想要其他选项,您可以逐行读取文本文件,并检查它是否以startsWith(...)和 the 来检索此行。

代码示例:

try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
    String line;
    while ((line = br.readLine()) != null)
    {
       // Add here 'if' condition and parse your line
    }
}

你的问题有点混乱。 你的标题说:

How do you allow a user to retrieve values from a list in Java?

你帖子的最后一行说:

How do I change that code so that it allows the user to retrieve
one single record from the text file?

它是来自列表还是来自文本文件?

如果它来自列表,因为您已经拥有可用的机制,那么它可能类似于:

public String getFlightInfo(String flightNumber) {
    List<Flight> flights = FlightBookingSystem.getFlights();
    for (Flight flite : flights) {
        if(flite.getFlightNumber().equalsIgnoreCase(flightNumber)){
            return flite.toString();
        }
    }
    JOptionPane.showMessageDialog(null, "<html>Flight number <font color=red><b>" 
            + flightNumber + "</b></font> could not be found!</html>", "Flight Not "
            + "Found", JOptionPane.INFORMATION_MESSAGE);
    return null;
}

上面的代码假设您有一个被覆盖的toString()方法应用于Flight类。 如果不这样做,则创建一个。

如果它实际上来自文件,那么它可能是这样的:

public String getFlightInfo(String flightNumber) {
    // 'Try With Resouces' to auto-close reader.
    try (BufferedReader reader = new BufferedReader(new FileReader("Flights.txt"))) {
        String fileLine = "";
        while ((fileLine = reader.readLine()) != null) {
            fileLine = fileLine.trim();
            // If by chance the file line read in is blank then skip it.
            if (fileLine.isEmpty()) {
                continue;
            }
            // First, remove the double colons at the end of line (if any). 
            if (fileLine.endsWith("::")) {
                fileLine = fileLine.substring(0, fileLine.lastIndexOf("::")).trim();
            }
            /* Split each read in file line based on a double colon delimiter. 
               The "\\s*" within the regex for the split method handles any 
               cases where the might be one or more whitespaces before or after 
               the double-colon delimiter.                */
            String[] lineParts = fileLine.split("\\s*\\:\\:\\s*");
            if(lineParts[1].equalsIgnoreCase(flightNumber)){
                // At this point you could just return the line, for example:
                // return fileLine;
                // or you can return a string with a little more structure:
                return new StringBuilder("Flight ID: ").append(lineParts[0])
                        .append(", Flight #: ").append(lineParts[1]).append(", From: ")
                        .append(lineParts[2]).append(", To: ").append(lineParts[3])
                        .append(", Date: ").append(lineParts[4]).toString();
            }
        }
    }
    catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    JOptionPane.showMessageDialog(null, "<html>Flight number <font color=red><b>" 
            + flightNumber + "</b></font> could not be found!</html>", "Flight Not "
            + "Found", JOptionPane.INFORMATION_MESSAGE);
    return null;
}

暂无
暂无

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

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