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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.