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
2
Contributors
1
Reply
19 Minutes
Discussion Span
1 Year Ago
Last Updated
2
Views
Related Article:Finding avg Length of String Arrays
is a Java discussion thread by JRDJ12 that has 8 replies, was last updated 1 year ago and has been tagged with the keywords: arrays, averages, looping, string-arrays.
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.