1
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));
}   
}
}
}

The above program shown the following output: Subject:Duminy Object: Warner Event: OUT Narrated Information: Duminy gets a wicket again. He has been breaking....

my question is, the text may contain example "Dumto to Warner, OUT, Duminy gets a wicket again. He has been breaking..." means, the above program wouldn't show output like above.. how to identity the text after the space for checking the condition

Taryn
  • 242,637
  • 56
  • 362
  • 405
Manivannan
  • 57
  • 3
  • 1
    You provided the example result you want but you didn't explain what is the purpose of your program... I can give you a solution that retrieves those strings getting the text on position X and still comply what you have asked for... – AlfaTeK Mar 03 '14 at 12:50

2 Answers2

1

You can read like this, but this is not encouraged. Take care.

    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]);
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
Maheshbabu Jammula
  • 357
  • 1
  • 2
  • 11
  • 2
    +1 - If you added `news = news.replace(",", "");` after readLine() and replaced `.split(" ");` with `.split(" ", 6);` this would be almost exactly what they asked for. If your quite alright with it, I will edit it into your answer. – Rudi Kershaw Mar 03 '14 at 12:35
  • 2
    @Rudi Kershaw good work – Maheshbabu Jammula Mar 03 '14 at 13:12
0
  • You have to find the first happening of " to ". All before it is the first string. All after it is the string with which we go on:
  • You have to find the first and the second happening of ",", thus dividing the remaining string into three parts: before first "," between "," "," and after the second ",".
  • The first part becomes the second string.
  • The second part becomes the third string.
  • The third part becomes the fourth string.
Gangnus
  • 24,044
  • 16
  • 90
  • 149