Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Character Count Frequently

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    Surabaya, Indonesia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Character Count Frequently

    Hello, i'm new here. I want create a character count frequently using SWING.

    it's my code
    import java.awt.BorderLayout;
     
     
    public class CharacterCounter extends JFrame {
     
    	private JPanel contentPane;
    	private JTextField input;
    	private JTextArea output;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					CharacterCounter frame = new CharacterCounter();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public CharacterCounter() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 274, 258);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		JLabel lblText = new JLabel("Text");
    		lblText.setBounds(34, 17, 70, 15);
    		contentPane.add(lblText);
     
    		input = new JTextField();
    		input.setBounds(77, 12, 164, 25);
    		contentPane.add(input);
    		input.setColumns(10);
     
     
     
    		JButton btnCalculate = new JButton("Calculate");
    		btnCalculate.addActionListener(new ActionListener() {
     
    			public void actionPerformed(ActionEvent arg0) {
     
    				String text = input.getText();
    				text = text.toLowerCase();
    				char [] character =  { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    				int [] freq = new int [character.length];
     
    				for ( int i = 0; i < text.length(); i++ ){
    					for ( int j = 0; j < character.length; j++ ){
    						if ( text.charAt(i) == character [j]) {
    							freq[j]++;
    						}
    					}
    				}
    				for ( int i = 0; i < freq.length; i++ ){
    					if (freq[i] >= 1) {
    						output.append(character[i]+"="+ freq[i]+ "\n");
    					}
    				}
    			}
     
    			}
    		);
    		btnCalculate.setBounds(141, 46, 100, 25);
    		contentPane.add(btnCalculate);
     
    		JTextArea output = new JTextArea();
    		output.setBounds(34, 83, 93, 133);
    		contentPane.add(output);
    	}
    }

    I've got error message when try to show the character count in text area (variable = output). Anyone can help?


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Character Count Frequently

    It'd be nice if you could print the message of the error here, and be more specific in your question.

    One thing that comes to my mind is that fact that you use the append() method... Can you think of a simpler way to add to a String?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Character Count Frequently

    Quote Originally Posted by snowguy13 View Post
    One thing that comes to my mind is that fact that you use the append() method... Can you think of a simpler way to add to a String?
    Its the append method of a JTextArea, so I'm not sure what your getting at

    Quote Originally Posted by ndundupan
    I've got error message when try to show the character count in text area (variable = output). Anyone can help
    I can guess this is a NullPointerException, but for future reference always provide as much information as possible. Add some println's in there to see what is null, and have a close look at which instance variables may not have been instantiated.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Location
    Surabaya, Indonesia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Character Count Frequently

    Thank for response my question. this almost resolve, the application using this code
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTextArea;
     
     
    public class CharacterCounter extends JFrame {
     
    	private JPanel contentPane;
    	private JTextField input;
    	private JTextArea output;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					CharacterCounter frame = new CharacterCounter();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public CharacterCounter() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 274, 258);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		JLabel lblText = new JLabel("Text");
    		lblText.setBounds(34, 17, 70, 15);
    		contentPane.add(lblText);
     
    		input = new JTextField();
    		input.setBounds(77, 12, 164, 25);
    		contentPane.add(input);
    		input.setColumns(10);
     
    		output = new JTextArea();
    		output.setBounds(34, 83, 93, 133);
     
     
     
    		JButton btnCalculate = new JButton("Calculate");
    		btnCalculate.addActionListener(new ActionListener() {
     
    			public void actionPerformed(ActionEvent arg0) {
     
    				String text = input.getText();
    				text = text.toLowerCase();
    				char [] character =  { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    				int [] frek = new int [character.length];
     
    				for ( int i = 0; i < text.length(); i++ ){
    					for ( int j = 0; j < character.length; j++ ){
    						if ( text.charAt(i) == character [j]) {
    							frek[j]++;
    						}
    					}
    				}
    				for ( int i = 0; i < frek.length; i++ ){
    					if (frek[i] >= 1) {
    						output.setText(String.valueOf(frek));
    //						output.append(character[i]+"="+ frek[i]+ "\n");
    					}
    				}
    			}
     
    			}
    		);
    		btnCalculate.setBounds(141, 46, 100, 25);
    		contentPane.add(btnCalculate);
     
    		contentPane.add(output);
    	}
    }

    but if i try to writing "welcome to java" and calculate it the result is there:
    009.jpg

    I want the result showing every character on every word. Sorry my english not good.

Similar Threads

  1. How to Count the CAPS
    By Sean137 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 14th, 2010, 08:30 AM
  2. Java Frequently asked questions
    By lakshmiv92 in forum The Cafe
    Replies: 1
    Last Post: October 19th, 2010, 05:20 AM
  3. select count(*)
    By jacinto in forum JDBC & Databases
    Replies: 4
    Last Post: March 2nd, 2010, 10:30 PM
  4. count how frequently each character occurs
    By sam in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:16 PM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM

Tags for this Thread