简体   繁体   中英

How do I scan multiple txt files at the same time?

I am learning Java EE and am trying to scan multiple text files based on the option value listed in my code. If one of the three options is selected it will scan a corresponding text file and output some information.

As you can see I have it working to scan one file while obviously ignoring the option selection. I am not sure how to adjust my code to switch to a different file based on the selection.

I think I will have to move some things around such as moving the while loop inside of an if statement that implements the option choice, and then scan the file for the id which shouldn't be an issue. I am just kind of dumbfounded on reading from different txt files.

I have posted my code below so you can get an idea of what I am trying to do.

Should I create multiple scanners and implement it that way or should I do something else?

Edit: Thanks to a little bit of help I have managed to get it working. Here is the revised working code.

<body>
    <%
    String id = request.getParameter("id");        
    String cid = request.getParameter("classID");
    String[] title = {"Name: ", "SSN: ", "Score: "};
    Scanner sc;        
    %>

    Find Your Current Score.
    <%-- Blank space here --%>
    <p></p>
    <%-- Input Forms (Text Field and Button) --%>
    <form action="ScoreFinder.jsp" method="get">
    Employee ID:
    <input type="text" name="id"/> Use SSN Format: xxx-xxx-xxxx
    <p></p>
    Class:
    <select name="classID">
    <option value="CMIS440">CMIS 440</option>
    <option value="CMIS445">CMIS 445</option>
    <option value="CMIS485">CMIS 485</option>
    </select>
    <input type="submit" value="Submit" />
    </form>

    <%
        if (id != null || cid != null) {
            String fileName = "/"+cid.replaceAll(" ", "") + ".txt";
            sc = new Scanner(new File(application.getRealPath(fileName)));
            while(sc.hasNext()) {                
            String line = sc.nextLine();
            String[] split = line.split("[#]");
            if (id.length() == 0) {
                out.println("Please enter an ID number.");
                break;
            }
            if (line.contains(id)) {
                out.println("Class: " + cid);
                %><br><%
                for (int i=0; i<split.length; i++){
                    out.println(title[i]);
                    out.println(split[i]);
                    %><br><%
                }
                break;
            }
            }
        }%>
</body>

Assuming your listed JSP's name is ScoreFinder.jsp and you are submitting records to the same JSP.

You can create the name of text file from the value of parameter id eg

<body>
    <%
    String id = request.getParameter("id");
    String fileName = "/"+id.replaceAll(" ", "")+".txt"; // avoiding null check of 'id' for simplicity
    Scanner sc = new Scanner(new File(application.getRealPath(fileName)));
    try {
        while(sc.hasNext()) {

    ........
    ........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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