Consider the code.. String s = "MINIMUM"; 1)System.out.println(s.substring(s.indexOf('I', 3))); 2)System.out.println(s.substring(s.indexOf('I', 4))); Answer 1) "INI" Answer 2) "INIM" Am I right.?
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
Why don't you write yourself a little test program, compile it, and run it, and then you can see for yourself what the answer is! Rob
Rob
SCJP 1.4
sonir shah
Ranch Hand
Joined: Nov 01, 2001
Posts: 435
posted
0
Rob, Actually this question was from jq+ and I was a bit confused because the answer for the first question was "MUM" A little bit confused.
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
The first one will return "IMUM" and the second throws a StringIndexOutOfBoundsException. You should write yourslef a test program and run it and verify the results yourself. Rob
patrick tang
Ranch Hand
Joined: Dec 16, 2001
Posts: 44
posted
0
s.indexOf('I', 3) means searching 'I' starting from index 3. as the second 'I' is index 3, it will return 3. s.indexOf('I', 4) will return -1 because there's no 'I' if searching from index 4. (remember index starts from 0). therefore first one will print IMUM and second one will give a RuntimeException.