【发布时间】: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 语句。有没有办法根据一行中的“
【问题讨论】:
标签: string if-statement text-files user-input keyword