如何在文本文件中查找特定单词,然后在Java中查找数字?

如何在文本文件中查找特定单词,然后在Java中查找数字?,java,file,search,text,Java,File,Search,Text,我有以下文本文件(answers.txt): 问题A:23 | 47 | 32 | 20 问题B:40 | 50 | 30 | 45 问题C:5 | 8 | 11 | 14 问题D:20 | 23 | 25 | 30 我需要的是能读懂我告诉它的问题(问题A,问题B),然后读懂后面的数字,这些数字用行隔开,然后像这样打印出来: public void read() { Scanner s = new Scanner("answers.txt"); ArrayList<String&g

我有以下文本文件(answers.txt):

问题A:23 | 47 | 32 | 20

问题B:40 | 50 | 30 | 45

问题C:5 | 8 | 11 | 14

问题D:20 | 23 | 25 | 30

我需要的是能读懂我告诉它的问题(问题A,问题B),然后读懂后面的数字,这些数字用行隔开,然后像这样打印出来:

public void read() {
  Scanner s = new Scanner("answers.txt");
  ArrayList<String> lines = new ArrayList<String>();
  while (s.hasNext()) {
    lines.add(s.nextLine());//first separate by line  
  }
  ProblemAnswer[] answerKey = new ProblemAnswer[lines.size()];
  for (int i = 0; i < lines.size(); i++) {
    String[] divide = lines.get(i).split(": "); //0 is the problem name, 1 is the list                 
                                                //of answers
    String[] answers = divide[1].split("|");  //an array of the answers to a given
                                              //question
    answerKey[i] = new ProblemAnswer(divide[0], answers); //add a new ProblemAnswer 
                                                          //object to the key
  }
}
问题A的答案:A.23 b.47 c.32 d.20


有人知道如何做到这一点吗?我在这上面呆了一段时间。

一行一行地读,先在“”处拆分。您将得到一个由“问题”、“A:”和“23 | 47 | 32 | 20”三部分组成的数组。然后在“|”处拆分第三部分,这样您将得到第二个包含四部分的数组“23”、“47”、“32”、“20”

合并所有内容以获得所需的输出

如果你想知道如何从文件中读取行或字符串,那么网上有数十亿个关于如何读取行或字符串的教程,所以我不会详细介绍如何读取。我相信你可以找到它们。

查看此代码! 它假定您具有以下文件格式:

Problem A:
23|7|32|20
Problem B:
40|50|30|45
Problem C:
5|8|11|14
Problem D:
20|23|25|30
因为你写了“后面的数字,用线隔开”

导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
导入java.util.Scanner;
公开课演示{
公共静态void main(字符串[]args)引发FileNotFoundException{
Scanner sc=新扫描仪(新文件(“answers.txt”);
List dataList=new ArrayList();
while(sc.hasNextLine()){
add(sc.nextLine());
}
System.out.println(数据列表);
Map Map=newhashmap();
对于(inti=0;我希望这有帮助

    public static void main(String args[])
{
    BufferedReader br = new BufferedReader(new FileReader(new File("answers.txt")));
    String lineRead  = null;
    String problem = "Problem A";//Get this from user Input
    List<String> numberData = new ArrayList<String>();
    while((lineRead = br.readLine())!=null)
    {
        if(lineRead.contains(problem))
        {
            StringTokenizer st = new StringTokenizer(lineRead,":");
            String problemPart = st.nextToken();
            String numbersPart = st.nextToken();
            st = new StringTokenizer(lineRead,"|");
            while(st.hasMoreTokens())
            {
                String number = st.nextToken();
                System.out.println("Number is: " + number);
                numberData.add(number);
            }
            break;
        }
    }
    System.out.println("Answers for " + problem + " : " + numberData );
}
publicstaticvoidmain(字符串参数[])
{
BufferedReader br=新的BufferedReader(新文件阅读器(新文件(“answers.txt”));
字符串lineRead=null;
String problem=“problem A”;//从用户输入中获取
List numberData=new ArrayList();
而((lineRead=br.readLine())!=null)
{
if(lineRead.contains(问题))
{
StringTokenizer st=新的StringTokenizer(lineRead,“:”);
字符串problemPart=st.nextToken();
字符串numbersPart=st.nextToken();
st=新的StringTokenizer(lineRead,“|”);
而(st.hasMoreTokens())
{
字符串编号=st.nextToken();
System.out.println(“编号为:”+Number);
numberData.add(数字);
}
打破
}
}
System.out.println(“对“+问题+”的回答:“+numberData”);
}

逐行阅读,用
拆分行:
。您将得到一个包含两部分的数组“问题A:”和“23 | 47 | 32 | 20”。然后在“|”拆分第二部分,这样您将得到包含四部分的第二个数组“23”、“47”、“32”、“20”

结合所有这些,您将得到您想要的输出


干杯!

使用
java.util.Scanner
可以过滤文件中的整数

Scanner s = new Scanner (new File ("answers.txt")).useDelimiter("\\s+");
while (s.hasNext()) {
    if (s.hasNextInt()) { // check if next token is integer
        System.out.print(s.nextInt());
    } else {
        s.next(); // else read the next token
    }
}

你知道如何逐行阅读吗?如果不知道,请检查

要细分字符串数据,有很多方法。可以根据需要细分。下面是我的代码

    String data = yourReader.readLine();
    String problem = data.substring("Problem".length(), data.indexOf(":"));
    System.err.println("Problem is " + problem);
    data = data.substring(data.indexOf(":") + 2, data.length());
    String[] temp = data.split("\\|");
    for (String result : temp) {
        System.out.println(result);
    }

假设始终有四个可能的答案,如您的示例所示:

// read complete file in fileAsString
String regex = "^(Problem \\w+): (\\d+)\\|(\\d+)\\|(\\d+)\\|(\\d+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(fileAsString);
//and so on, read all the Problems using matcher.find() and matcher.group(int) to get the parts
// put in a Map  maybe?
// output the one you want...

我可能建议为组织目的创建一个简单的数据类型:

public class ProblemAnswer {
  private final String problem;
  private final String[] answers;

  public ProblemAnswer(String problem, String[] answers) {
    this.problem = problem;
    this.answers = new String[answers.length];
    for (int i = 0; i < answers.length; i++) {
      this.answers[i] = answers[i];
    }
  }

  public String getProblem() {
    return this.problem;
  }

  public String[] getAnswers() {
    return this.answers;
  }

  public String getA() {
    return this.answers[0];
  }

  public String getB() {
    return this.answers[1];
  }

  public String getC() {
    return this.answers[2];
  }

  public String getD() {
    return this.answers[3];
  }
}
公共类问题答案{
私有最终字符串问题;
私有最终字符串[]答案;
公共问题答案(字符串问题,字符串[]答案){
这个问题=问题;
this.answers=新字符串[answers.length];
for(int i=0;i
然后,从文本文件读取的内容如下所示:

public void read() {
  Scanner s = new Scanner("answers.txt");
  ArrayList<String> lines = new ArrayList<String>();
  while (s.hasNext()) {
    lines.add(s.nextLine());//first separate by line  
  }
  ProblemAnswer[] answerKey = new ProblemAnswer[lines.size()];
  for (int i = 0; i < lines.size(); i++) {
    String[] divide = lines.get(i).split(": "); //0 is the problem name, 1 is the list                 
                                                //of answers
    String[] answers = divide[1].split("|");  //an array of the answers to a given
                                              //question
    answerKey[i] = new ProblemAnswer(divide[0], answers); //add a new ProblemAnswer 
                                                          //object to the key
  }
}
public void read(){
扫描仪s=新扫描仪(“answers.txt”);
ArrayList行=新的ArrayList();
而(s.hasNext()){
lines.add(s.nextLine());//首先按行分隔
}
ProblemAnswer[]answerKey=新问题答案[lines.size()];
对于(int i=0;i
现在,您只需要一个带有ProblemAnswer对象的答案键,它很容易检查
通过对
getProblem()
方法进行简单的
.equals()
比较,以及匹配的任何索引,您可以将所有答案整齐地排列在同一个对象中。

您可以发布您迄今为止尝试过的代码吗?这不会将“问题C:”分为“问题”和“C:?那里”映射。get(“问题C:);“不是”问题C::