We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,089 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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)
2
Contributors
1
Reply
19 Minutes
Discussion Span
1 Year Ago
Last Updated
2
Views
shean1488
Junior Poster in Training
68 posts since Feb 2012
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0

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.

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0638 seconds using 2.77MB