-4

So, the full String that I'm trying to read is:

Members online: (0/0):
Members offline: (0/0):

While I think I could substring it to get the 0, it can change it's size, because it's an int... Is there any way I could get the integers even if they change? Thanks.

EDIT: I also want to only get the first integer of "0/0"... Any ways I could do this? I know I could use regex but I don't know if it's the best way to do it...

SantiBailors
  • 1,596
  • 3
  • 21
  • 44
John M
  • 23
  • 2
  • 5
  • Why dont you try it with regular expressions. just google it – kism3t Mar 13 '17 at 12:17
  • Possible duplicate of [Get int from String, also containing letters, in Java](http://stackoverflow.com/questions/2338790/get-int-from-string-also-containing-letters-in-java) – takendarkk Mar 13 '17 at 12:18
  • 1
    You should never use regex for task like this. Just get indexOf('(') and indexOf('/') and indexOf(')') and use Integer.parseInt with proper substring – Jack Mar 13 '17 at 12:20
  • 1
    @Jack, that depends on many factors, including the need for making sure the string has the expected format. I think my first shot would be a regex, then I might consider changing later if I encounter reasons for doing so. – Ole V.V. Mar 13 '17 at 12:46
  • Nah, @takendarkk, it’s hardly a strict duplicate. In that other question there is only one number in the string, and its format is not known. – Ole V.V. Mar 13 '17 at 12:47
  • Thanks for the help guys. I've solved my problem :) – John M Mar 13 '17 at 13:57

1 Answers1

1

You should use indexOf and lastIndexOf for that purpose.

int n1 = Integer.parseInt(t.substring(t.indexOf('(') + 1, t.indexOf('/')))

That variable (n1) will save the number itself, if you want just the position you should use next one.

int pos=t.indexOf('(') + 1