Avatar of gudii9
gudii9
Flag for United States of America asked on

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
Java EEJavaProgramming Languages-Other

Avatar of undefined
Last Comment
gudii9

8/22/2022 - Mon
gudii9

ASKER
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
SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
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
ozo

String[] words=text.split("");
is still missing a space, it should be
String[] words=text.split(" ");
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
String[] words=text.split("");
is still missing a space, it should be
String[] words=text.split(" ");

after this fix works fine
gudii9

ASKER
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