import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException
{
File fis=new File("D:/Testcode/Test.txt");
BufferedReader br;
String input;
String var = null;
if(fis.isAbsolute())
{
br=new BufferedReader(new FileReader(fis.getAbsolutePath()));
while ((input=br.readLine())!=null) {
var=input;
}
}
//String var="Duminy to Warner, OUT, Duminy gets a wicket again. He has been breaking...
if(var!=null)
{
String splitstr[]=var.split(",");
if(splitstr[0].contains("to"))
{
String ss=splitstr[0];
String a[]=ss.split("\\s+");
int value=splitstr[0].indexOf("to");
System.out.println("Subject:"+splitstr[0].substring(0,value));
System.out.println("Object:"+splitstr[0].substring(value+2));
System.out.println("Event:"+splitstr[1]);
int count=var.indexOf(splitstr[2]);
System.out.println("Narrated Information:"+var.substring(count));
}
}
}
}
上面的程序显示了以下输出: 主题:虚拟 对象:华纳 事件: 出局 叙述信息:杜米尼再次获得三柱门。他已经打破了......
我的问题是,文本可能包含示例“Dumto to Warner,OUT,Duminy gets a wicket again. He has been Break...”意味着,上面的程序不会像上面那样显示输出..如何识别空格后的文本以检查条件
最佳答案
您可以这样阅读,但不鼓励这样做。保重。
File read=new File("D:\\Test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(read),Charset.forName("UTF-8")));
String news = reader.readLine();
news = news.replace(",", "");
String[] names = news.split(" ", 6);
System.out.println("First String : "+names[0]+" "+names[1]);
System.out.println("Second String : "+names[3]);
System.out.println("Third String : "+names[4]);
System.out.println("Fourth String : "+names[5]);
关于java - 如何读奇数位置的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144643/