Rakesh A
Rakesh A

Reputation: 3

Extraction of numerals from a string

I need to extract the numerals in a given string and store them in a separate array such that each index stores a individual number in the string. Ex-"15 foxes chases 12 rabbits". I need the numbers 15 and 12 to be stored in a[0] and a[1].

 String question=jTextArea1.getText();
 String lower=question.toLowerCase();
 check(lower);

 public void check(String low)
  {
 int j;
 String[] ins={"into","add","Insert"};  
 String cc=low;

 for(int i=0;i<2;i++)
  {    
 String dd=ins[i];
 if(cc.contains(dd))
  {
      j=1;
      insert(cc);
      break;
  }
  }}

public void insert(String low)
{ 
String character = low;
int l=low.length();
int j[]=new int[20];
int m=0;

for(int k=0;k<=2;k++)
 {
   j[k]=0;
   for(int i=0;i<l;i++)
  { 
    char c = character.charAt(i);
    if (Character.isDigit(c)) 
  {

       String str=Character.toString(c);
       j[k]=(j[k]*10)+Integer.parseInt(str);
       m++;
  } 
  else if (Character.isLetter(c))
  {
    if(m>2)
  {
     break;
 }
 }}} 

Upvotes: 0

Views: 88

Answers (4)

Robert Wadowski
Robert Wadowski

Reputation: 1427

I believie it should be

    String str = "12 aa 15 155";

    Scanner scanner = new Scanner( str );
    while( scanner.hasNext() )
    {
        if( scanner.hasNextInt() )
        {
            int next = scanner.nextInt();
            System.out.println( next );
        }
        else
        {
            scanner.next();
        }
    }
    scanner.close();

Upvotes: 0

aidonsnous
aidonsnous

Reputation: 1503

Regex is the best option for you.

    //Compilation of the regex
    Pattern p = Pattern.compile("(\d*)");
    // Creation of the search engine
    Matcher m = p.matcher("15 foxes chases 12 rabbits");
    // Lunching the searching 
    boolean b = m.matches();
    // if any number was found then
    if(b) {
        // for each found number
        for(int i=1; i <= m.groupCount(); i++) {
            // Print out the found numbers;
    // if you want you can store these number in another array 
//since m.group is the one which has the found number(s)
            System.out.println("Number " + i + " : " + m.group(i));
        }
}

You must import java.util.regex.*;

Upvotes: 1

Naji Kadri
Naji Kadri

Reputation: 99

To extract numerals from Strings use the following code:

   String text = "12 18 100";
   String[] n = text.split(" ");
   int[] num = n.length;
   for (int i =0; i < num.length;i++) {
     num[i] = Integer.parseInt(n[i]);
    };

All the string numbers in the text will be then integers

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

Assuming you can't use a Collection like a List, I would start by splitting on white space, using a method to count the numbers in that array (with a Pattern on digits), and then build the output array. Something like

public static int getNumberCount(String[] arr) {
    int count = 0;
    for (String str : arr) {
        if (str.matches("\\d+")) {
            count++;
        }
    }
    return count;
}

And then use it like

public static void main(String[] args) {
    String example = "15 foxes chases 12 rabbits";
    String[] arr = example.split("\\s+");
    int count = getNumberCount(arr);
    int[] a = new int[count];
    int pos = 0;
    for (String str : arr) {
        if (str.matches("\\d+")) {
            a[pos++] = Integer.parseInt(str);
        }
    }
    System.out.println(Arrays.toString(a));
}

Output is (as requested)

[15, 12]

Upvotes: 0

Related Questions