-2

I have this convertor which is used to get or set label from a combo box:

public class GroupConverter extends StringConverter<ListGroupsObj>
{

    @Override
    public String toString(ListGroupsObj obj)
    {
        return obj.getGroupId() + " - " + obj.getGroupName();
    }

    @Override
    public ListGroupsObj fromString(String obj)
    {

        Scanner fi = new Scanner(obj);
        //anything other than alphanumberic characters, 
        //comma, dot or negative sign is skipped
        fi.useDelimiter("[^\\p{Alnum},\\.-]");
        while (true)
        {
            if (fi.hasNextInt()){
                //System.out.println("Int: " + fi.nextInt());
                return ListGroupsObj.newInstance().groupId(fi.nextInt());
            }
            break;                    
        }

        //TODO when you type for example "45 - NextGroup" you want to take only tyhe number"
        return ListGroupsObj.newInstance().groupId(fi.nextInt());
    }

}

For example when I type "45 - NextGroup" I would like to get only the number from the value. Can you help me implement this?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • if obj.getGroupId() returns a String, you can simply do `Integer.parseInt(obj.getGroupId());` – Kon Dec 22 '13 at 21:15
  • Similar to http://stackoverflow.com/questions/2367381/extract-numbers-from-a-string-java – MariuszS Dec 22 '13 at 21:15

2 Answers2

1

try this

String s="45 - Next";// the string from the box
int num=Integer.parseInt(s.split("-")[0].trim());
Dima
  • 8,586
  • 4
  • 28
  • 57
1
        String s="45 - Next";
        String reg = "[0-9]+";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(s);
        while(matcher.find()) {
            String numString = matcher.group();
            int num = Integer.parseInt(numString);
            System.out.println(num);
        }
Longwayto
  • 396
  • 2
  • 6