gudii9
asked on
java substring(1,2) issue
Hi,
I am working on below challenge
http://codingbat.com/prob/p151713
I wrote as below
I passed one test and failed other test cases as below
Expected Run
mixStart("mix snacks") → true false X
mixStart("pix snacks") → true false X
mixStart("piz snacks") → false false OK
mixStart("nix") → true false X
mixStart("ni") → false false OK
mixStart("n") → false Exception:java.lang.String IndexOutOf BoundsExce ption: String index out of range: 2 (line number:3) X
mixStart("") → false Exception:java.lang.String IndexOutOf BoundsExce ption: String index out of range: 2 (line number:3) X
I thought substring(1,2) will check for string"ix" and simply retun true in that case. Looks like something is wrong with my assumption. How to improve above code. Please advise
I am working on below challenge
http://codingbat.com/prob/p151713
I wrote as below
public boolean mixStart(String str) {
if((str.substring(1,2)).equals("ix")){
return true;
}
return false;
}
I passed one test and failed other test cases as below
Expected Run
mixStart("mix snacks") → true false X
mixStart("pix snacks") → true false X
mixStart("piz snacks") → false false OK
mixStart("nix") → true false X
mixStart("ni") → false false OK
mixStart("n") → false Exception:java.lang.String
mixStart("") → false Exception:java.lang.String
I thought substring(1,2) will check for string"ix" and simply retun true in that case. Looks like something is wrong with my assumption. How to improve above code. Please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
return str.indexOf("ix") == 1;
what is meaning of above line? Does it simply check if 'ix' is there in the given string 'str'?
Please advise
I do not see any specific requirement for small strings of 2 or 1 etc characters in the challenge.It's not spelled out, but they are sending in some short strings to test the behavior.
Give this a try and then see if you can see why it fixed it:
public boolean mixStart(String str) {
if (str.length() > 2 && (str.substring(1,3)).equal
return true;
}
return false;
}
And this line:
str.indexOf("ix") == 1;checks to see if "ix" is within the string...and also if it's in position 1. Can you see how that would solve this too?
Doug
ASKER
public boolean mixStart(String str) {
if (str.length() > 2 && (str.substring(1,3)).equals("ix")){
return true;
}
return false;
}
solving all test cases
Also
public boolean mixStart(String str) {
return str.indexOf("ix") == 1;
}
above also passing all test casesAnd this line:
str.indexOf("ix") == 1;
checks to see if "ix" is within the string...and also if it's in position 1.
ix position check 1 means checking the starting position of i right(which is the first letter in ix?)
Please advise
Do you understand the documentation for the indexOf function?
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf%28java.lang.String%29
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf%28java.lang.String%29
ASKER
I was reading it now.
what it means by above line?
The returned index is the smallest value k for which:
k >= fromIndex && this.startsWith(str, k)
what it means by above line?
ix position check 1 means checking the starting position of i right(which is the first letter in ix?)Yes that's right.
It's the position in the string where the substring ("ix") starts.
k >= fromIndex
means k is greater than or equal to k >= fromIndex
this.startsWith(str, k) is documented here:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String,%20int%29
The returned index is the smallest value k for which:
means that there is no smaller value for which this is true
i.e. if there is more than one value which satisfies it, we choose the smallest one.
means k is greater than or equal to k >= fromIndex
this.startsWith(str, k) is documented here:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String,%20int%29
The returned index is the smallest value k for which:
means that there is no smaller value for which this is true
i.e. if there is more than one value which satisfies it, we choose the smallest one.
ASKER
i need to read more
ASKER
i never used regular expressions. I wonder i can do programming without knowing regular expression? May be it is easy to learn regular expression with java background. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window
when i tried as above i have some test cases failing as below
Expected Run
mixStart("mix snacks") → true true OK
mixStart("pix snacks") → true true OK
mixStart("piz snacks") → false false OK
mixStart("nix") → true true OK
mixStart("ni") → false Exception:java.lang.String
mixStart("n") → false Exception:java.lang.String
mixStart("") → false Exception:java.lang.String
I do not see any specific requirement for small strings of 2 or 1 etc characters in the challenge.
Return true if the given string begins with "mix", except the 'm' can be anything, so "pix", "9ix" .. all count.
mixStart("mix snacks") → true
mixStart("pix snacks") → true
mixStart("piz snacks") → false
Please advise