Results 1 to 11 of 11
  1. #1
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default how to get the Integers out of a String

    Hi,

    I have a string like this

    heads tails heads tails 10 5

    How can I get the 10 and the 5 into seperate variables

    e.g.

    headCount=10

    tailsCount=5

    I need to get all the integers out of a String

    is it possible?

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    yes, use regular expressions.

    take a look at String.matches() in java doc
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default

    I found it but I don't really understand regular Expressions, can anyone give me an example?

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    examples? not me, its a whole chapter by itself.
    if only christ was here...

    try googleing for it. java regex
    here's one:
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    String one="10";
    int two=Integer.parseInt(one);

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    err no. its more like:
    String s = "heads tails heads tails 10 5";

    and he wants to extract 10 and 5 out of it.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default

    Figured out a way to do it

    String s="heads tails heads tails 10 5";

    String[] arraystring=s.split(" ");
    int anint = String[4]; //which will be 10
    int anotherint=String[5]; //which will be 5

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    OH its a fixed string. i thought you have a dynamic string.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    
    public class Numbers {
    
    	/**
    	 * @param args
    	 */
    	public int[] hasNumber(String s) {
    	      int[] numbs=new int[s.length()];
    	      int count=0;
    	      for (int j = 0;j < s.length();j++) {
    	         if (Character.isDigit(s.charAt(j))){
    	           numbs[count]=Integer.parseInt(Character.toString(s.charAt(j)));
    	            for(int i=j+1;Character.isDigit(s.charAt(i));i++){
    	                   j=i;    
    	                   numbs[count]=numbs[count]*10+Integer.parseInt(Character.toString(s.charAt(j)));
    	                   
    	             }  
    	            count++;
    	           }
    	       
    	    }
    	   return numbs;
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new Numbers().hasNumber("123fdgdf78hj");
    
    	}
    
    }

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    thats a good idea to avoid regex.
    actually the string must end w/ a non-digit, otherwise, it'll throw an exception.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Here's a way to use a regex to get rid of text. You would then split, as you did, and not have to worry about the position. Given that you are dealing with fixed position, this is unnecessary, but learning regex will save you in a lot of situations.

    String workString = s.replaceAll("[^\\d ]", "");
    workString = workString.replaceAll("(^ +| +$"), "");

    The first expression removes everything but digits and blanks. Note the the \\d has two backslashes because Java uses backslash as an escape character, the regex only sees \d.

    The second expression removes leading and training spaces.

    Go to a regex tutorial site and find out what all that mess does. It's a good way to start.

Similar Threads

  1. Replies: 1
    Last Post: 10-20-2008, 08:55 PM
  2. Finding Median of X Integers
    By Hasan in forum New To Java
    Replies: 3
    Last Post: 08-12-2008, 03:06 PM
  3. How do I strip down integers for future manipulation?
    By frasifrasi in forum New To Java
    Replies: 12
    Last Post: 06-14-2008, 03:17 AM
  4. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 06:49 PM
  5. Convert roman numerals to integers
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 12:27 AM

Posting Permissions

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