java - 如何读奇数位置的单词

标签 java string bufferedreader inputstreamreader

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/

相关文章:

java - 调用 JaxWsProxyFactoryBean.create() 时 apache cxf 出现异常(在 maven 项目中)

python - 使用 str.format_map() 时超出最大字符串递归

c - 将 "String"拆分为 C 中的字符

java - 使用java读取文本文件,eclipse开普勒

java - java中动态追加语音识别的语法文件

JavaFX "is already set as root of another scene"

Java:如何返回 ArrayList 中的唯一元素

Java:尝试将 int 值转换为 String

java - 如果我有 key ,如何从 map 中获取值?

java - 如何使用子字符串从 bufferedreader 中提取文本