Discussion:
For loop on Word List
(too old to reply)
s***@gmail.com
2013-01-23 16:01:30 UTC
Permalink
Dear Group,

I am trying to learn Java.

If I have a string like,
"Moscow is the capital of Russia",
I like to see it as,

["Moscow","is","the","capital","of","Russia"]

"Moscow",0
"is",1
"the",2
"capital",3
"of",4
"Russia",5

the string into a bag of words, and each word and its corresponding index.

If any one can help me.

Regards,
Subhabrata.
Eric Sosman
2013-01-23 16:30:54 UTC
Permalink
Post by s***@gmail.com
Dear Group,
I am trying to learn Java.
If I have a string like,
"Moscow is the capital of Russia",
I like to see it as,
["Moscow","is","the","capital","of","Russia"]
"Moscow",0
"is",1
"the",2
"capital",3
"of",4
"Russia",5
the string into a bag of words, and each word and its corresponding index.
If any one can help me.
Since this sounds very much like homework, I won't offer
a fully worked-out solution. Instead, I'll suggest that you
look at the split() method of the String class (there are two
split() methods, but I think the one-argument version is better
for the purpose you describe).
--
Eric Sosman
***@comcast-dot-net.invalid
s***@gmail.com
2013-01-23 17:26:41 UTC
Permalink
Post by s***@gmail.com
Dear Group,
I am trying to learn Java.
If I have a string like,
"Moscow is the capital of Russia",
I like to see it as,
["Moscow","is","the","capital","of","Russia"]
"Moscow",0
"is",1
"the",2
"capital",3
"of",4
"Russia",5
the string into a bag of words, and each word and its corresponding index.
If any one can help me.
Regards,
Subhabrata.
Dear Sir,
Thank you for the help. I worked out the following solution.
It seems working.

public class For_loop {
public static void main(String args[]) {
String mystring="Subhabrata Banerjee works in India";
String[]word=mystring.split(" ");
int l2=word.length;
int l1=mystring.length();

for(int x = 0; x < l2; x = x+1) {
System.out.print("value of x : " + x );
System.out.println(word[x]);
System.out.print("\n");
}
}
}

Regards,
Subhabrata.
markspace
2013-01-24 01:33:55 UTC
Permalink
Post by s***@gmail.com
Thank you for the help. I worked out the following solution.
It seems working.
public class For_loop {
public static void main(String args[]) {
String mystring="Subhabrata Banerjee works in India";
String[]word=mystring.split(" ");
System.out.println( Arrays.toString( word ) );
Post by s***@gmail.com
}
}
Not exactly the same thing, but the point is that there are built-in
methods to do most common tasks in Java. In general your code is a lot
simpler if you use them.
Roedy Green
2013-01-24 03:40:47 UTC
Permalink
Post by s***@gmail.com
["Moscow","is","the","capital","of","Russia"]
to break up you string into words see
http://mindprod.com/jgloss/regex.html you want the split method.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
Loading...