Results 1 to 4 of 4
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default get values from JTextFields input

    Hi.
    I have written small program that works with coordinates.
    I want to use JTextField for insertion coords like this: "(x,y)".

    So my first problem is how to get values x and y from string "(x,y)".
    My first idea is to get rid of brackets and then split string with .split(',').
    Is it good approach, or there is some better way to do it?

    My second problem is how to make my input more flexible.
    For example somethimes I'll have input: "(x, y)" or " ( x, y ) ", and I still want to get values from it.

    Is it good to work with regular expressions in this case?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    split is fine and using a regex to get the values is ok too.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    PhHein thanks very much.

    I solved the problem.
    Here is my code:

    Java Code:
    import java.util.regex.Pattern;
    
    public class GetCoordValues {
    	
    	public static void main(String[] args) {
    		String coord = "(12   ,   14  )";
    		String[] coords;
    		
    		coord = coord.substring(1, coord.length()-1);
    		Pattern pattern = Pattern.compile("[,\\s]+");
    		coords = pattern.split(coord);
    		for(String variable : coords)
    			System.out.println(variable);
    					
    	}
    }

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Uhm, you're welcome, but you solved it yourself indeed.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Changing Webpage's Input Values
    By Lawllerskates in forum New To Java
    Replies: 0
    Last Post: 05-16-2010, 02:26 AM
  2. draw in jpanel using values input in textbox
    By New to JAVA in forum AWT / Swing
    Replies: 6
    Last Post: 07-08-2009, 11:06 AM
  3. Please help me. Input Values sending from Jsp to Servlet
    By Santoshbk in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-17-2009, 02:18 PM
  4. Problem with JTextFields not null
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:17 AM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •