简体   繁体   English

Java 如何在 txt 文件中显示和搜索相同的值,以便扫描并显示所有记录(如果值在记录中)

[英]Java How can I display and search the same value in a txt file so all the records are scanned and displayed if value is in the record

Here is my quest, I have the same value in records in a text file.这是我的任务,我在文本文件中的记录中具有相同的值。 But I only get to find the first one and to display the first one.但我只能找到第一个并显示第一个。 How can I find all the record with that value and display them.如何找到具有该值的所有记录并显示它们。 Here is the code,这是代码,

package projectsby;

import java.io.File;
import java.util.Scanner;
import javax.swing.JOptionPane;

    public class Find {
    private static Scanner o;

        public void openFile(){
            try{
                o = new Scanner(new File("/Users/e.h.j.hofman/NetBeansProjects/Projectsby/src/projectsby/Projects.txt"));
            }

    catch(Exception e) {
                System.out.println("Could not find file");   
    }}

        /**
         *
         * @param searchTerm
         * @param filepath
         */
        public void readEntries(String searchTerm, String filepath){

        boolean found = false;
        String Project = "";
        String omschrijving = "";
        String BU= ""; 
        String afdeling = "";

        try
        {
            o = new Scanner(new File(filepath));
            //o.useDelimiter("[ \n]");
            while(o.hasNext()
                    //&& !found )
            {

                Project = o.next();
                omschrijving = o.next();
                BU = o.next();
                afdeling = o.next();

                if(Project.equalsIgnoreCase(searchTerm))
                {
                    found = true; }
            }
           if (found) 
           {

Here i suspect it will help when there is some kind of while loop.在这里,我怀疑当有某种 while 循环时它会有所帮助。

JOptionPane.showMessageDialog
(null,"Project: " + Project + " " +omschrijving + "\nBU: " 
+ BU + "\nAfdeling: " +afdeling);
           } else {
                       JOptionPane.showMessageDialog(null,"Project not found");
        }}
        catch(Exception e)
      {
            JOptionPane.showMessageDialog(null, "Error");
      }}
    public void closeFile(){
        o.close();
      }}

It is a little hard to tell from your code formatting but the block of code in the "if (found)" statement will only be executed at the end of the file since it is outside the while loop.从您的代码格式中很难分辨,但“if (found)”语句中的代码块只会在文件末尾执行,因为它位于 while 循环之外。 Maybe that code should be moved to the "if(Project.equalsIgnoreCase(searchTerm))" statement instead.也许应该将该代码移至“if(Project.equalsIgnoreCase(searchTerm))”语句。 Then it will execute on every record in the file, if the searchTerm is found.如果找到了 searchTerm,它将在文件中的每条记录上执行。

Then there is no reason for the "found" boolean.那么就没有理由“找到”boolean。

 while (o.hasNext()) {
    ...
    if(Project.equalsIgnoreCase(searchTerm)) {
        JOptionPane.showMessageDialog ...
    }
 }

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

相关问题 如何从 java 中的 txt 文件中获取特定值? - How can I get a specific value from txt file in java? Java:如何清除和排序 .txt 文件中的所有内容? - Java : How can I clear and sorting all contents in .txt file? 如何从java MVC中的arraylist .TXT文件显示JTable中的记录? - How to display records in a JTable from an arraylist .TXT file in java MVC? 如何扫描文件以寻找价值 - How can I Scan file to search for value 如何将每条记录与同一List Java中的所有其他记录进行比较? - How to compare each record with all other records in a same List Java? 如何创建/格式化.txt文件以便eclipse / java可以读取它? - how do I create/format a .txt file so that eclipse/java can read it? 如何在Java中将txt文件转换为数组,以便我可以分别处理每个数组 - How to convert a txt file into array in Java so I can deal with every array separately 如何从 Scanner 中输入 Value,并将 output 的值输入到 Java 中的 txt 文件中? - How input Value from Scanner, and output the value to an txt file in Java? 如何搜索所有列并更新特定值SQLite - How can I search all Columns and Update specific value SQLite Java:如何将txt文件中的多个句子拆分为一个数组,然后在数组中搜索单个单词? - Java: How can I split multiple sentences in a txt file to an array and then search for individual words in the array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM