string split method

Hi,

import java.util.Arrays;


public class Words {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String text;
		text="aa bb cc dd"
		+"ee ff"
		+"gg hh";
		String[] words=text.split("");
		//Arrays.sort(words);
		int count=0;
	for (String word : words) {
		System.out.println("words are-->"+word);		
	}
			
		}

	}

Open in new window


also when i type for then  control space i see foreach in eclipse when i select it put for loop only not foreach.
i wonder foreach not coming

i am trying above program got below output
words are-->
words are-->a
words are-->a
words are--> 
words are-->b
words are-->b
words are--> 
words are-->c
words are-->c
words are--> 
words are-->d
words are-->d
words are-->e
words are-->e
words are--> 
words are-->f
words are-->f
words are-->g
words are-->g
words are--> 
words are-->h
words are-->h

Open in new window


Not sure why it is not splitting at space pproperly giving out put like
aa then bb

Also i do not see with or without sort no difference. please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gudii9Author Commented:
when i was typing
TreeMap<String, Integer> tm new T
and then i clicked sontrol space i do not see option of
TreeMap<String, Integer>
as attached.  i wonder why it does show that when i click control space
TreeMap.jpg
0
ozoCommented:
why it is not splitting at space
Because there is no space in your call to the split method
String[] words=text.split("");
should be
String[] words=text.split(" ");
0
gudii9Author Commented:
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class Words {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String text;
		text="aa bb cc dd"
		+"aa ee ff"
		+"gg hh dd";
		String[] words=text.split("");
		//Arrays.sort(words);
		int count=0;
	for (String word : words) {
		System.out.println("words are-->"+word);		
	}
			
	
	TreeMap<String, Integer> wordList=new TreeMap<String,Integer>();
	String thisWord="";
	String nextWord="";
	for (int i = 0; i < words.length; ++i) {
		thisWord=words[i];
		count++;
		nextWord=words[i++];
		if (!thisWord.equals(nextWord)) {
			wordList.put(thisWord, count);
			count=0;
		}
			}
	
	//odd
	nextWord=words[words.length-1];
	if ((thisWord.equals(nextWord))) {
		count++;
	}
	else {
		count=1;
	}
	
	if (words.length %2!=0) {
		wordList.put(nextWord, count);
		
	}
	
	Set set=wordList.entrySet();
	Iterator i=set.iterator();
	while (i.hasNext()) {
		Map.Entry me=(Map.Entry)i.next();
		System.out.println(me.getKey()+"; ");
		System.out.println(me.getValue()+"; ");
		
	}
	}

	}

Open in new window


i improved my code to include and compare even and odd.

i still get wrong output as below

words are-->
words are-->a
words are-->a
words are--> 
words are-->b
words are-->b
words are--> 
words are-->c
words are-->c
words are--> 
words are-->d
words are-->d
words are-->a
words are-->a
words are--> 
words are-->e
words are-->e
words are--> 
words are-->f
words are-->f
words are-->g
words are-->g
words are--> 
words are-->h
words are-->h
words are--> 
words are-->d
words are-->d

Open in new window


please advise
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ozoCommented:
String[] words=text.split("");
is still missing a space, it should be
String[] words=text.split(" ");
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
also when i type for then  control space i see foreach in eclipse when i select it put for loop only not foreach.
i wonder foreach not coming
I can see that may be confusing... The "foreach" is the label that Eclipse is putting on a particular template. That label and the corresponding keyword that is produced from the template does NOT have to be the same. Java doesn't actually have a "foreach" keyword, it uses the same keyword "for" for two different types of loops. ie. for (int i = 0; i < length; i++)     is one style and    for (String word : wordList)      is another style. Common labels that people call those two constructs and "for loop" and "foreach loop" respectively, but it doesn't mean that Java has a foreach keyword.

i do not see option of
TreeMap<String, Integer>
as attached.  i wonder why it does show that when i click control space
That is just the way that content assist works. It doesn't bother putting the Generic arguments in the dropdown list because there is no real value. The dropdown list only shows classes/methods that may be appropriate in the current context, but when you do select one, it will still add the generic arguments for you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
String[] words=text.split("");
is still missing a space, it should be
String[] words=text.split(" ");

after this fix works fine
0
gudii9Author Commented:
i see two options like below suggested by eclipse which one to take which scenario. please advise
TreeMap<Integer, String> ts=new TreeMap<Integer, String>(theComparator);
		
		TreeMap<Integer, String> tm=new TreeMap<Integer, String>();

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.