Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2467 > unrolled thread

For loop on Word List

Started bysubhabangalore@gmail.com
First post2013-01-23 08:01 -0800
Last post2013-01-23 19:40 -0800
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.help


Contents

  For loop on Word List subhabangalore@gmail.com - 2013-01-23 08:01 -0800
    Re: For loop on Word List Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-23 11:30 -0500
    Re: For loop on Word List subhabangalore@gmail.com - 2013-01-23 09:26 -0800
      Re: For loop on Word List markspace <markspace@nospam.nospam> - 2013-01-23 17:33 -0800
    Re: For loop on Word List Roedy Green <see_website@mindprod.com.invalid> - 2013-01-23 19:40 -0800

#2467 — For loop on Word List

Fromsubhabangalore@gmail.com
Date2013-01-23 08:01 -0800
SubjectFor loop on Word List
Message-ID<43165a8a-3a3b-4869-aa7e-3c4e4bbc5898@googlegroups.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. 

[toc] | [next] | [standalone]


#2468

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-01-23 11:30 -0500
Message-ID<kdp37v$5ms$1@dont-email.me>
In reply to#2467
On 1/23/2013 11:01 AM, subhabangalore@gmail.com wrote:
> 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
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#2469

Fromsubhabangalore@gmail.com
Date2013-01-23 09:26 -0800
Message-ID<e7e6d308-c7aa-4fc6-a92e-0d6dff7d2884@googlegroups.com>
In reply to#2467
On Wednesday, January 23, 2013 9:31:30 PM UTC+5:30, subhaba...@gmail.com wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#2470

Frommarkspace <markspace@nospam.nospam>
Date2013-01-23 17:33 -0800
Message-ID<kdq32l$4gt$1@dont-email.me>
In reply to#2469
On 1/23/2013 9:26 AM, subhabangalore@gmail.com wrote:

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

> 	   }
> 	}


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.




[toc] | [prev] | [next] | [standalone]


#2471

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-01-23 19:40 -0800
Message-ID<h6b1g8da6segb358ovsnnno24qpuuc5n85@4ax.com>
In reply to#2467
On Wed, 23 Jan 2013 08:01:30 -0800 (PST), subhabangalore@gmail.com
wrote, quoted or indirectly quoted someone who said :

>["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 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web