【问题标题】:Need java code in importing a text file comparing a set of strings在导入比较一组字符串的文本文件时需要java代码
【发布时间】:2017-12-09 23:15:40
【问题描述】:

我有一组字符串 (string1,string2,string3,string4),我想使用 java 编码在文本文件 (input.txt) 中搜索它,我尝试使用以下命令但它不起作用,请帮助.

package string;
import java.io.File; //Required for input file
import java.io.FileNotFoundException; //Required for exceptioenter code heren throw
import java.util.Scanner; //required for scanner

public class strng {

public static void main(String[] args) throws FileNotFoundException // throws clause added
{

//ask the the for the string to be searched
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter part of the string: ");
    String searchString = keyboard.next().toLowerCase();

 // open the data file
    File file = new File("APPD_01_15_16_01.txt");
 // create a scanner from the file
    Scanner inputFile = new Scanner (file);



    // While there is another line to read.
    while(inputFile.hasNext())
    {
        // read the lines
         //Read string

        //Check if user input is a match and if true print out info.

        if(searchString.contains("samplemachine")
        {
            System.out.println("Yup!");
        }
        else
        {
            System.out.println("Fail!");
        }
    }

    // be polite and close the file
    inputFile.close();

}

}

【问题讨论】:

  • 您从 inputFile 中读取的字符串在哪里?
  • 您的示例代码有点令人困惑,因为您似乎将用户键盘输入与您搜索“samplemachine”的字符串进行比较,而不是文件中的输入。
  • 我尝试从用户输入中搜索,但我的要求是从字符串集中搜索
  • 但它不起作用不是一个足够好的解释
  • 你能把你的文件内容贴出来吗?这将有很大帮助。

标签: java string java.util.scanner java-io


【解决方案1】:

据我所知,这将满足您的需求。

public static void main(String[] args) throws IOException //Alternatively, you should probably handle this.
{
    //I don't know what your source is for the search strings,
    //but this will simulate them.
    String[] toSearch = {"Strings", "to", "search", "for."};
    //Simplest way to read a file when no editing is needed.
    for (String line : Files.readAllLines(FileSystems.getDefault().getPath("input.txt")))
    {
        for (String str : toSearch)
        {
            //Simplest way to check if one String contains another.
            if (line.contains(str))
            {
                System.out.println("Yup!");
                //Now that we know the file contains a String, no need to continue.
                return;
            }
        }
    }
    //If it even gets to this point, it got through the file with no match.
    System.out.println("Fail!");
}

其他一些答案涉及类似的方法,但我觉得我的答案更完整地涵盖了整个问题,而不是它的特定部分。

【讨论】:

    【解决方案2】:

    您的代码没有按照您所说的去做。如果您的要求是从一组字符串中进行搜索,那么为什么要与从用户那里获取的输入进行比较?

    【讨论】:

      【解决方案3】:

      根据您的代码,您没有阅读 inputFile 的内容。您应该在循环内逐行读取 inputFile 的内容。阅读该行后,您现在可以检查该行是否包含您要查找的内容。

      // While there is another line to read.
      while(inputFile.hasNextLine())
      {
          // read the next line from the input file
          String line = inputFile.nextLine();
      
          //Check if user input is a match and if true print out info.
          if(line.contains(searchString)
          {
              System.out.println(line);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-06
        • 1970-01-01
        • 2021-03-16
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多