【问题标题】:Creating if/else statements, and scanners based on the keyword in as string.基于字符串中的关键字创建 if/else 语句和扫描器。
【发布时间】:2018-06-07 21:27:52
【问题描述】:

我正在尝试让我的代码搜索特定关键字,并根据这些特定关键字创建扫描仪用户输入提示来替换这些关键字。 比如在txt文件中:

你好,我的名字是,你叫什么名字?你的名字是吗?

我喜欢吃。你呢?

程序应检测到“”并提示用户为不同的关键字输入两次名称。

到目前为止,我有这个:

// Java program to illustrate reading from Text File
// Using scanner class
import java.io.File;
import java.util.Scanner;

public class TxtOutput{
public static void main(String[] args) throws Exception
      {
        // pass the path to the file as a parameter
        File file = new File("C:\\Users\\aaron\\Documents\\TestTXT\\test.txt");
        Scanner sc = new Scanner(file);
        //Types of keywords
        //<adjective>, <plural-noun>, <place>, <noun>, <funny-noise>, <person's-name>, <job>, <CITY>, , <Color!>
        //, <Exciting-adjective>, <Interersting-Adjective>, <aDvErB>, <NUMBER>, <Plural-noun>, <body-part>, <verb>, 
        //<Number>, <verB>, <job-title>, 
        String data1 = sc.nextLine();

        if (data1.contains("<job>"));
        Scanner user_input = new Scanner (System.in);
        String job1;
        System.out.println("Enter a profession");
        job1 = user_input.next(); 
        String replacedData1 = data1.replace("<job>", job1 ); 
        System.out.println(replacedData1);
      }


}

该程序只能检测一个关键字,并且它具有预先制作的 if 和 else 语句。有没有办法根据一行中的“”或“”等关键字使用扫描仪制作 if 和 else 语句? 我不想用不必要的预制 if 和 else 语句来轰炸这个程序。我想知道是否有更有效的方法来做到这一点。

【问题讨论】:

    标签: string if-statement text-files user-input keyword


    【解决方案1】:

    您可以使用正则表达式在整个数据文件中搜索&lt;..&gt; 关键字模板,将找到的关键字添加到唯一的Set,然后遍历关键字以请求替换。我想你喜欢这样:

    我建议像这样在正则表达式中使用 | 交替明确指定关键字模板:

    &lt;adjective&gt;|&lt;plural-noun&gt;|&lt;place&gt;|&lt;noun&gt;|&lt;funny-noise&gt;|&lt;person's-name&gt;|&lt;job&gt;|&lt;CITY&gt;|&lt;Color!&gt;|&lt;Exciting-adjective&gt;|&lt;Interersting-Adjective&gt;|&lt;aDvErB&gt;|&lt;NUMBER&gt;|&lt;Plural-noun&gt;|&lt;body-part&gt;|&lt;verb&gt;|&lt;Number&gt;|&lt;verB&gt;|&lt;job-title&gt;

    Demo

    我们可以使用像&lt;[^&lt;&gt;]+&gt; 这样的通用正则表达式,但我不知道您的文件中还有什么。试试看吧。

    把所有东西放在一起,complete sample

    import java.util.*;
    import java.lang.*;
    import java.io.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    class Ideone {
     public static void main(String[] args) throws java.lang.Exception {
      Set < String > uniqueKeywords = new HashSet < String > ();
      final String regex = "<adjective>|<plural-noun>|<place>|<noun>|<funny-noise>|<person's-name>|<job>|<CITY>|<Color!>|<Exciting-adjective>|<Interersting-Adjective>|<aDvErB>|<NUMBER>|<Plural-noun>|<body-part>|<verb>|<Number>|<verB>|<job-title>";
      final String filecontent = "Text template containing all sorts of .. <adjective>, <plural-noun>, <place>, <noun>, <funny-noise>, <person's-name>, <job>, <CITY>, , <Color!> <Exciting-adjective>, <Interersting-Adjective>, <aDvErB>, <NUMBER>, <Plural-noun>, <body-part>, <verb>,  <Number>, <verB>, <job-title>, String data1 = sc.nextLine(); blah blah";
    
      final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
      final Matcher matcher = pattern.matcher(filecontent);
    
      while (matcher.find()) {
       uniqueKeywords.add(matcher.group(0));
      }
    
      Scanner user_input = new Scanner(System.in);
      for (String keyword: uniqueKeywords) {
       System.out.println("Enter a " + keyword);
       String replacement = user_input.next();
       String replacedData1 = filecontent.replace(keyword, replacement);
       System.out.println(replacedData1);
      }
     }
    }
    

    【讨论】:

    • 我使用了通用的正则表达式。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多