1.11M Members

Help me please to organise String.format


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)

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 six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article