1,105,384 Community Members

Help me please to organise String.format

Member Avatar
Junior Poster in Training
67 posts since Feb 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

Hi everybody,

In my little programm, that prompts user to choose txt file, and then displays how many each particular token in the file. I have a little issue with organising the ouput. I think I do something wrong with String.Format, can you please help me:

I attached my ouput.

But I would like numbers to be in line.

here is the method that displays output:

//display map
	public String displayFileMap() {
		String str = new String();
		//returns a Set view of the keys contained in map
		Set<String> keys = map.keySet();
		str +=("\nMap contains elemets: \nKeys\t\tValues");
		//Sort keys
		Set<String> sortedKeys = new TreeSet<String>(keys);
		//print each key and its value 
		for(String key : sortedKeys)
			str+=String.format("\n%-10s%10d", key, map.get(key));
		// display the map size 		
		str+= String.format("\n\n%-10s%10d\n%-10s%11b",
				"size of map is :", map.size(),
				"map isEmpty: ",map.isEmpty() );
		return str;

And here is the whole program if you want to check it

Attachments Screenshot_at_2012-03-12_13:23:12.png 18.66KB WordCounter.java (2.1KB) WordCounterFrame.java (0.37KB) WordCounterPanel.java (0.9KB)
Member Avatar
Posting Whiz
376 posts since Oct 2007
Reputation Points: 42 [?]
Q&As Helped to Solve: 60 [?]
Skill Endorsements: 9 [?]

try changing the font of the component you are printing on for a monospaced font, your code seems good, but the character widths is what is causing you numbers not to be aligned.

This article has been dead for over three months: Start a new discussion instead
Start New Discussion
Tags Related to this Article