Help me please to organise String.format

 
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.66 KB WordCounter.java (2.1 KB)
/**Theis program prompts user to open file
 * then it counts and displays number of tokens.
 * 
 * @author Sergey Dobrovolskiy
 * 
 */


import java.util.*;
import javax.swing.*;

public class WordCounter {
	
	// main map Keys are elements and Values are number of elements 
	private Map<String, Integer> map = new HashMap<String, Integer>(); 
	private Scanner file;
	
	public static void main(String[] args) {
		WordCounterFrame wcf = new WordCounterFrame();
		
	}
		
	public WordCounter() {
		
		//openFile(); 
		//createFileMap();
		//displayFileMap();
	}
	
	// this method opens file
	public void openFile() {
		JFileChooser myChooser = new JFileChooser();
		int status = myChooser.showOpenDialog(null);
		
		try {
			file = new Scanner(myChooser.getSelectedFile());
		}
		catch (Exception e){
			System.err.println("Can not open file");
			System.exit(0);
		}
	}
	
	//creates a Map
	public void createFileMap() {
		//read the file, line by line
		while(file.hasNextLine()) {
			// put the current line into str
			String str = file.nextLine();
		
			// breaks a line into tokens
			StringTokenizer tokenizer = new StringTokenizer(str); 
		
			//check if there are more tokens
			while(tokenizer.hasMoreTokens()) { 
				// assign current token to the word
				String word = tokenizer.nextToken(); 
			
				/*if Map has current word add 1 to the maps value
				  if no then associated current word with value 1*/
				if(map.containsKey(word)) {
					int count = map.get(word);
					map.put(word, count +1);
				}
				else
					map.put(word, 1);
			}
		}
	}
	
	//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;
	}
}
WordCounterFrame.java (0.37 KB)
import javax.swing.*;
import java.awt.*;

public class WordCounterFrame extends JFrame{
	public WordCounterFrame() {

		WordCounterPanel wcp = new WordCounterPanel();
		this.getContentPane().add(wcp, BorderLayout.CENTER);
				
		this.setSize(300, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(300,100);
		this.setVisible(true);
				
	}

}
WordCounterPanel.java (0.9 KB)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WordCounterPanel extends JPanel implements ActionListener {
	
	JButton openFile_B;
	JTextArea fileText_TA;
	WordCounter wc = new WordCounter();
	
	public WordCounterPanel() {
		openFile_B = new JButton("Choose txt File");
		fileText_TA = new JTextArea();
		fileText_TA.setBackground(new Color(0xF5F5DC));
		fileText_TA.setFont(new Font("Serif", Font.PLAIN, 15));
		
		this.add(openFile_B);
		this.add(fileText_TA);
		
		openFile_B.addActionListener(this);
	}
	public void chooseFile() {
		wc.openFile();
	}
	public void printAnalyse() {
		wc.createFileMap();
		fileText_TA.setText(wc.displayFileMap());
	}

	public void actionPerformed(ActionEvent eve) {
		String actionCommand = eve.getActionCommand();
		if(actionCommand.equals("Choose txt File")) {
			chooseFile();
			printAnalyse();
		}
	}
}
 
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.

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