【问题标题】:How to read odd number position words [closed]如何阅读奇数位置词[关闭]
【发布时间】:2014-04-04 09:01:27
【问题描述】:
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));
}   
}
}
}

上述程序显示以下输出: 主题:杜米尼 对象:华纳 事件:OUT 叙述信息:杜米尼再次获得检票口。他一直在崩溃……

我的问题是,文本可能包含示例“Dumto to Warner, OUT, Duminy got a wicket again. He has been broken...” 意思是,上面的程序不会像上面那样显示输出.. 如何识别检查条件的空格后面的文本

【问题讨论】:

  • 你提供了你想要的示例结果,但你没有解释你的程序的目的是什么......我可以给你一个解决方案,检索那些字符串,获取位置 X 上的文本并且仍然遵守你的要求...

标签: java string bufferedreader inputstreamreader


【解决方案1】:
  • 您必须找到“to”的第一次发生。之前都是the first string。之后就是我们继续使用的字符串:
  • 您必须找到“,”的第一次和第二次出现,从而将剩余的字符串分成三部分:在第一个“,”之前,在“,”之间,在第二个“,”之后。
  • 第一部分变为the second string
  • 第二部分变成the third string
  • 第三部分变为the fourth string

【讨论】:

    【解决方案2】:

    您可以这样阅读,但不鼓励这样做。保重。

        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]);
    

    【讨论】:

    • +1 - 如果您在 readLine() 之后添加 news = news.replace(",", ""); 并将 .split(" "); 替换为 .split(" ", 6); 这几乎正是他们所要求的。如果您对此表示满意,我会将其编辑到您的答案中。
    • @Rudi Kershaw 干得好
    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多