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 5 of 5

Thread: Replacing dots with spaces

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Replacing dots with spaces

    When I give in for example a String called : This.is.a.test and press the convert button , the textfield becomes empty.
    I used String function : replace all to replace the dots with spaces.
    When I set the text of the textfield with a normal string(" ") it works but with a variable it doesnt work.

    Thanks in advance

    Heres my code :

    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
    public class TestReplace {
     
    	private JFrame frame;
    	private JTextField inputString;
    	String s;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					TestReplace window = new TestReplace();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public TestReplace() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(100, 100, 450, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
     
    		inputString = new JTextField();
    		inputString.setBounds(67, 88, 307, 34);
    		frame.getContentPane().add(inputString);
    		inputString.setColumns(10);
     
    		JLabel lblGeefStringIn = new JLabel("String :");
    		lblGeefStringIn.setBounds(173, 55, 108, 14);
    		frame.getContentPane().add(lblGeefStringIn);
     
    		JButton btnNewButton = new JButton("Convert");
    		btnNewButton.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
    				s = inputString.getText();
    				s = s.replaceAll(".", " ");
    				inputString.setText(s);
    			}
    		});
    		btnNewButton.setBounds(162, 150, 108, 23);
    		frame.getContentPane().add(btnNewButton);
    	}
    }
    Last edited by dTi; November 17th, 2011 at 08:41 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Replacing dots with spaces

    I have no idea what your problem is. You need to provide a better explanation. Don't reply "Just run my code and you will see what I mean". If you can explain it clearly then I don't need to waste my time copying your code, compiling it and running it.
    Improving the world one idiot at a time!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Replacing dots with spaces

    Try resizing the GUI. If your text shows up after that then you have a painting issue.
    Improving the world one idiot at a time!

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Replacing dots with spaces

    Ahhh, I just realised. The replaceAll method uses a regular expression and the dot has a special meaning in regex. Try escaping the dot.
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    dTi (November 17th, 2011)

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Replacing dots with spaces

    I didn't know that.
    Anyway if you use replace instead of replaceAll it works .
    If you need replaceAll you need to escape values.
    Thanks for your help.

Similar Threads

  1. [SOLVED] Replacing letters in a string NOT WORKING.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2011, 06:38 PM
  2. [SOLVED] replacing elements in a string
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2011, 07:31 PM
  3. Cant get spaces
    By Ksmartman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 07:06 AM
  4. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM
  5. dispose()'ing a JFrame and replacing it
    By musasabi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 14th, 2010, 02:31 PM