Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag 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
Avatar of gudii9
gudii9
Flag of United States of America image

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
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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
String[] words=text.split("");
is still missing a space, it should be
String[] words=text.split(" ");
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of gudii9

ASKER

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

after this fix works fine
Avatar of 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