• Post Reply Bookmark Topic Watch Topic
  • New Topic

indexing and get particular word from the document  RSS feed

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have an sentence and need to get the output in the middle of the word before the sentence ends

i have an sentence like this

It was hot (so hot!) I'm telling you that from yesterday and i am getting an output like this
currrent output:
previous output:It was hot (
current output:so hot!
next output: ) I'm telling you that from yesterday

but i need output like this for the same sentence. like this
Required output:
previous output:It was hot (
current output:so hot!
next output: ) I'm telling you

the coding of the program is
import java.io.*;
import java.lang.*;
import java.util.regex.*;

public class ex {
public static void main(String[] args)
{
try{
String string = "It was hot (so hot!) I'm telling you that from yesterday";

int left = string.indexOf("(");
int right = string.indexOf(")");

// pull out the text inside the parens
String sub = string.substring(left+1, right); // sub is "so hot!"

//System.out.println(sub);

//sub = sub.toUpperCase(); // sub is "SO HOT!"

// Put together a new string
String result =
string.substring(0, left+1); // It was hot (
// sub ; // SO HOT!
String s= string.substring(right);
System.out.println("previous output:"+result); // ) I'm telling you.
System.out.println("current output:"+sub);
System.out.println("next output:"+s);

}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}
}


can any one tell me how to get this output like this from that sentence or is there any different code can be done to get the output what i require?
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mailed my doubt yesterday.but still i have not got the reply for that.can any one help me atleast now??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not at all clear what your requirements are.

i have an sentence and need to get the output in the middle of the word before the sentence ends

i have an sentence like this

It was hot (so hot!) I'm telling you that from yesterday and i am getting an output like this
currrent output:
previous output:It was hot (
current output:so hot!
next output: ) I'm telling you that from yesterday

but i need output like this for the same sentence. like this
Required output:
previous output:It was hot (
current output:so hot!
next output: ) I'm telling you


I have no idea what your rule is that makes the second one correct but not the first one. Can you please try to explain more clearly and precisely?

Also, when you post code, please UseCodeTags(⇐click) so it will be readable.
 
Tim Moores
Saloon Keeper
Posts: 4030
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is "next output" defined? Currently it's everything to the right of the parenthesis; can you describe in words what it should be?
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i need the next output as like this

next output: ) I'm telling you

i'm getting an output like this now

next output: ) I'm telling you that from yesterday

i don't want the words "that from yesterday " from the output window i need to remove that input from that sentence. now can any one tell me how to remove that words after "i'm telling you"?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote: i don't want the words "that from yesterday "

Yes, but we still don't understand why. How does the program know what you want and what you don't?

That's what you need to explain to us, in detail.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote:hi,

i need the next output as like this

next output: ) I'm telling you

i'm getting an output like this now

next output: ) I'm telling you that from yesterday

i don't want the words "that from yesterday " from the output window i need to remove that input from that sentence. now can any one tell me how to remove that words after "i'm telling you"?



You still haven't told us the rules, however.

Here is one possible solution. It perfectly meet the requirements you have given. However, I doubt it fits your actual requirements, but since you haven't told us those, that can't be helped.





 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no i have an sentence like this

It was hot (so hot!) I'm telling you that from yesterday

and i have an program like this
import java.io.*;
import java.lang.*;
import java.util.regex.*;

public class ex {
public static void main(String[] args)
{
try{
String string = "It was hot (so hot!) I'm telling you that from yesterday";

int left = string.indexOf("(");
int right = string.indexOf(")");

// pull out the text inside the parens
String sub = string.substring(left+1, right); // sub is "so hot!"

//System.out.println(sub);

//sub = sub.toUpperCase(); // sub is "SO HOT!"

// Put together a new string
String result =
string.substring(0, left+1); // It was hot (
// sub ; // SO HOT!
String s= string.substring(right);
System.out.println("previous output:"+result); // ) I'm telling you.
System.out.println("current output:"+sub);
System.out.println("next output:"+s);

}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}
}

i am getting an output for the program like this
currrent output:
previous output:It was hot (
current output:so hot!
next output: ) I'm telling you that from yesterday


i need the last output line to be changed as currrent output:
previous output:It was hot (
current output:so hot!
next output: ) I'm telling you that from yesterday

to
currrent output:
previous output:It was hot (
current output:so hot!
next output: ) I'm telling you

in the coding part i have to remove that words"that from yesterday".

and i didn't ask it for getting output like this

# System.out.println("previous output:It was hot (");
# System.out.println("current output:so hot!");
# System.out.println("next output: ) I'm telling you");

now have you understood the requirment?
i don't want that word i want to remove it. can you tell me how to do that?


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote: i don't want that word i want to remove it. can you tell me how to do that?

One way:
string = string.replace(" that from yesterday", "");
although I doubt whether that's much different from Jeff's suggestion.

now have you understood the requirement?

I suspect not, because you haven't explained it. All you've done is repeat yourrself.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have said exactly the same thing now, three times. You have not offered one bit of clarification.

Imagine I was looking for help and I said this:

I have the String "ABCdefGHI" but I need to strip out "def" and get just "ABCGHI"


Both of the following would meet that requirement, but neither of them is what I want:


What I really wanted is "get rid of all characters between the start token "ABC" and the end token "GHI". But because I didn't explain my requirements clearly, nobody would be able to help me. Just like nobody can help you because you're note explaining your requirements. You just keep repeating over and over, 'I have the String "ABCdefGHI" but I need to strip out "def" and get just "ABCGHI"'.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

my requirement is to to the extraction part from the code. i need to extract the code at first part and some parts in the middle and remove that last part.as i told in that sentence. it's is to extract the words from sentence and remove any words after the output what i require is needed.now can you understand my requirment ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote:
my requirement is to to the extraction part from the code. i need to extract the code at first part and some parts in the middle and remove that last part.as i told in that sentence. it's is to extract the words from sentence and remove any words after the output what i require is needed.now can you understand my requirment ?


I don't understand the issue here -- the methods that you used so far, the index() and substring() methods which you used superbly to extract the first two components, can be used to extract what you want from the third component. What is it about this last requirement that is tripping you so much when compared to the earlier requirements?

Henry

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote:hi,

my requirement is to to the extraction part from the code.


Yes, we know this. It is not new information.

i need to extract the code at first part


Define "the first part."

First 20 characters? Up to the first newline? Third whitespace?

and some parts in the middle


And it really doesn't occur to you to explain which parts? So when you were given this assignment, the person giving to you just said, "extract some parts in the middle"?

now can you understand my requirment ?


No. You have now said the same thing 4 times, with no clarification.

Unless of course your question is really, "How do I extract the middle or end part of a String?" because you have no idea how to extract any part of a String at all. But that seems unlikely, since your code shows that you clearly know about the existence of some of the methods that can be used for this, and you said you're able to get it to do part of what you want.

So the only thing that makes sense is you're having trouble using those methods to apply your particular rules. But since you haven't told us those rules, we can't help you.
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Article by Tom Kyte (aka Oracle's "Ask Tom") about how to ask questions: http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Winston,

i have tried out your method but it is giving the same output.so i'm not able to get the output what i expect. And Jeff your coding works perfectly for the input you have given
String s2 = s1.substring(0, 3) + s1.substring(6, 9);
and
String s2 = s1.replaceAll("[a-z]++", "");

but it is used to strip the middle words only.i need to extract the output for this sentence:
It was hot (so hot!) I'm telling you that from yesterday omit the last sentence lines. because the indexof is used to print the output for start and end of line. But i need it for start and middle of line output. and not the last part of output.If i take it for an document how can i use those number parameters such as substring(0,3).it won't suit for an document na.so i'm asking how to remove the last few words and get the extracted output. now can any one help me ?
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first part in that sentence is "It was hot (so hot! "

i get output like this
previous output:It was hot (
current output:so hot!

that part has no problem.
middle part is ") I'm telling you"

and last part is "that from yesterday "

i need to strip that part "that from yesterday".Sorry for i'm not telling you the clear concepts.i'm sorry for the inconvinence and thank's for your reply.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote:hi Winston,

i have tried out your method but it is giving the same output.so i'm not able to get the output what i expect. And Jeff your coding works perfectly for the input you have given
String s2 = s1.substring(0, 3) + s1.substring(6, 9);
and
String s2 = s1.replaceAll("[a-z]++", "");




That was not meant to solve your problem. It was meant to illustrate an example on how you're not communicating your problem.

But i need it for start and middle of line output.


Define "start" and "middle". Precisely. In way that covers every possible input text you might get. If you don't, it's impossible to solve your problem.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote:first part in that sentence is "It was hot (so hot! "

i get output like this
previous output:It was hot (
current output:so hot!

that part has no problem.
middle part is ") I'm telling you"

and last part is "that from yesterday "

i need to strip that part "that from yesterday".


You have now said the same thing at least 5 times. Saying it over and over again does not make it any clearer.

Sorry for i'm not telling you the clear concepts.


So make it clear.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote:first part in that sentence is "It was hot (so hot! "...

Yes, we've got that...many times over now.

You are dealing with a group of intelligent people here, and yet we cannot help you.
Why do you suppose that is?

Winston
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He must do this on purpose, to see who's going to break first
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me leave this sentence .

i'll use some other example

i'm using an sentence
String sentence ="<p>Kalam sees the world without fear</p>.<span> He spent most of his childhood running into financial problems and started working at an early age to supplement his family's income.</span> <Religion>Kalam was brought up in a multi-religious, tolerant society, with Kalam strictly following his religious routine- – he would start his day by getting up at 4:00 am; study and review his homework before going to school to attend the mathematics class as his first class of the day..</Religion>.<span>After completing school, Kalam along with his cousin Samsuddin Kalam distributed papers in order to financially contribute to his father's income</span>";

i'm framing an html tags for an sentence and i need to get the output alone for that sentence

so i use pattern matching for those sentence and if it matches the sentence it gives the output

then i need to split the sentences separately like the following using indexof. i'm using indexof syntax because it used to split the tags seperate from the sentence and get the required output by splitting words
int left = string.indexOf("<span>");
int right = string.indexOf("</span>");
and i get the following output as following

<p>Kalam sees the world without fear</p>.<span> He spent most of his childhood running into financial problems and started working at an early age to supplement his family's income.</span> <Religion>Kalam was brought up in a multi-religious, tolerant society, with Kalam strictly following his religious routine- – he would start his day by getting up at 4:00 am; study and review his homework before going to school to attend the mathematics class as his first class of the day..</Religion>.<span>After completing school, Kalam along with his cousin Samsuddin Kalam distributed papers in order to financially contribute to his father's income</span>

span> He spent most of his childhood running into financial problems and started working at an early age to supplement his family's income.

and
: </span> <Religion>Kalam was brought up in a multi-religious, tolerant society, with Kalam strictly following his religious routine- – he would start his day by getting up at 4:00 am; study and review his homework before going to school to attend the mathematics class as his first class of the day..</Religion>.<span>After completing school, Kalam along with his cousin Samsuddin Kalam distributed papers in order to financially contribute to his father's income</span>

but from the above sentence i don't want to have sentence fully.i require only this sentence

: </span> <Religion>Kalam was brought up in a multi-religious, tolerant society, with Kalam strictly following his religious routine.

and strip the rest of sentences after that words.that is my goal.i need to get the output match from the document for the same.so initially i require to make it simple i give two sentences to split the words and get the output. so now i think i'm much clearer than before?
now can you tell me how to proceed? or you need the coding for better reference?

 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijayalakshmi,

We all knows that you need to have some sub string from a string, but to fetch that sub string from the big string we need to have some rule(you might want to fetch n character or up to a particular keyword found or till the first full stop etc). Like when you have first string "sentence" in your program you are breaking it on the basis of "<span>" keyword and you are getting two strings, so here you define the rule that "break it from "<span>" word".

Now you want to slash out the rest of the part of your second sub string that you are having. So, you are required to tell after what length/word etc the remaining part of the string has no use for you and you want to ignore it. you might say that ignore that rest of the part when "--" occurs (as you splinted out the string here like that) or you can say ignore the rest of the part after 10 words.

What I think you might not able to convey but you want to have(however I am not sure):

1. First split the string on the basis of "<span>" or something. you will have two strings.
2. Take the second string and read it till first shortest sentence completes according to English grammar. (I don't know how to define complete sentence)

Its all my assumption, sorry if it's wrong.

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi dude..

thanks' for your reply.i'll look ahead with this as of now.. thank's a lot.....
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayalakshmi deepika wrote:now can you tell me how to proceed?

Ah, now we're getting somewhere.

Java has a number of parsers for HTML, and my strong suggestion would be to use one of those to extract the text (Personally, I'd look at SAX-based one first, as it's the simplest (and fastest I believe); you could try JTidy, but there are plenty of others).

Substrings and regexes are OK for straight matches, but very poor for hierarchical tagged input like HTML

Winston
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm changind the regular expression and able to get the required output and not by the substring usage.because if the sub string has an very long sentence it's some what i feel it's prolong the data ans not able to get the output. so i change the regular expression and get that output. if any one able to get it with substring kindly tell me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!