Solved

java substring(1,2) issue

Posted on 2014-10-05
13
495 Views
Last Modified: 2014-10-07
Hi,

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;
}

Open in new window


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.StringIndexOutOfBoundsException: String index out of range: 2 (line number:3)      X         
mixStart("") → false      Exception:java.lang.StringIndexOutOfBoundsException: 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
0
Comment
Question by:gudii9
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 167 total points
ID: 40362574
You're very close.

substring(1,2) returns the string starting at index 1 and ending at index 2 - so it's just checking 1 character, not 2.

Try this instead:

public boolean mixStart(String str) {
  if ((str.substring(1,3)).equals("ix")){
  return true;
  }
  return false;
}

It's still not quite correct.  You'll get the exceptions about "index out of range" when passing in a short string.

Can you see how to fix those cases?

Doug
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 166 total points
ID: 40362612
Substringing is unnecessary:

public boolean mixStart(String str) {
    return str.indexOf("ix") == 1;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40362787
public boolean mixStart(String str) {
  if ((str.substring(1,3)).equals("ix")){
  return true;
  }
  return false;
}

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.StringIndexOutOfBoundsException: String index out of range: 3 (line number:2)      X         
mixStart("n") → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 3 (line number:2)      X         
mixStart("") → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 3 (line number:2)      X       



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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:gudii9
ID: 40362790
return str.indexOf("ix") == 1;

Open in new window


what is meaning of above line? Does it simply check if 'ix' is there in the given string 'str'?
Please advise
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40362801
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)).equals("ix")){
  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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40362810
public boolean mixStart(String str) {
  if (str.length() > 2 && (str.substring(1,3)).equals("ix")){
  return true;
  }
  return false;
}

Open in new window


solving all test cases


Also

public boolean mixStart(String str) {
    return str.indexOf("ix") == 1;
}

Open in new window

above also passing all test cases


And 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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40362815
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40362819
I was reading it now.

The returned index is the smallest value k for which:

 k >= fromIndex && this.startsWith(str, k)

what it means by above line?
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40362821
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40362828
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40362837
i need to read more
0
 
LVL 7

Author Comment

by:gudii9
ID: 40366368
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
0
 
LVL 84

Accepted Solution

by:
ozo earned 167 total points
ID: 40366545
You can do  programming without knowing regular expressions.
You can do  programming without knowing the java.math library.
Although awareness of them can be generally helpful.
Without them some things may become more inconvenient to do, and in solving some problems
you may find yourself reinventing them, perhaps in non-standard ways that make it harder
for you to communicate your solutions with others.
And even if the problem at hand is not one in which use of either is appropriate,
a general awareness of the landscape can give one a better perspective.
But there are still a lot of places to explore even avoiding particular areas.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 49
eclipse buid path vs tomcat lib path 10 24
jboss wildfly 10.1 10 98
pagenation logic how it is working in my code 1 31
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question