Solved

lastTwo challenge

Posted on 2014-12-15
18
75 Views
Last Modified: 2014-12-31
Hi,

I am working on below challenge
http://codingbat.com/prob/p194786

I wrote my code as below
public String lastTwo(String str) {
int len=str.length();
if(len>=2){
  return str.substring(0,len-2)+str.substring(len-1)+str.substring(len-2,len-1);
  }
  
}

Open in new window


I am failing with below message
Compile problems:


Error:      public String lastTwo(String str) {
                    ^^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

see Example Code to help with compile problems

How to fix and improve my code. Thanks in advance
0
Comment
Question by:gudii9
  • 8
  • 6
  • 2
  • +2
18 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 125 total points
ID: 40501495
0
 
LVL 84

Expert Comment

by:ozo
ID: 40501534
What does your code do when len>=2 is not true?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40501677
Challenge did not specify for case when length less than 2. Should I assume blank string. Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40501693
Challenge tests include
lastTwo("a") → "a"      
lastTwo("") → ""
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 125 total points
ID: 40501700
You didn't read or didn't grasp the question properly, because it explicitly says that you are given a string of any length. "Any" must include zero. Another clue is that the Q says "last two chars, if present . . . "
0
 
LVL 7

Author Comment

by:gudii9
ID: 40501849
public String lastTwo(String str) {
int len=str.length();
if(len>=2){
  return str.substring(0,len-2)+str.substring(len-1)+str.substring(len-2,len-1);
  }
  else{
  return "";
  }
  
}

Open in new window


when i wrote as above failing test case as below for single character.
Expected	Run		
lastTwo("coding") → "codign"	"codign"	OK	    
lastTwo("cat") → "cta"	"cta"	OK	    
lastTwo("ab") → "ba"	"ba"	OK	    
lastTwo("a") → "a"	""	X	    
lastTwo("") → ""	""	OK	    
other tests
OK	   

Open in new window


i do not see any info regarding single character string in the challenge. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40502025
I see info in the challenge test that says
lastTwo("a") → "a"

Reading between the lines of the challenge description, one might suppose that if two characters are not present in the input string, then no swapping is performed on it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40502603
lastTwo("a") → "a"

I see above only in the test results section. Not in the challenge description.

As you mentioned i took care of that case also by keeping same character
public String lastTwo(String str) {
int len=str.length();
if(len>=2){
  return str.substring(0,len-2)+str.substring(len-1)+str.substring(len-2,len-1);
  }
  if(len==1){
  return str.substring(0);
  }
  else{
  return "";
  }
  
}

Open in new window


i passed all tests. Does my code looks fine?
any improvements to it. Please advise
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40502622
Not in the challenge description.

But it DOES! It says ANY length, and that must include the possibility of zero length.

All you needed here :
if(len==1){
  return str.substring(0);
  }

Open in new window

is
if(len==1){
return str;
}

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Assisted Solution

by:awking00
awking00 earned 125 total points
ID: 40509165
Another possibility is to use StringBuilder
public String lastTwo(String str) {
StringBuilder sb = new StringBuilder(str);
int len=sb.length();
if(len>=2){
  char lastChar = sb.charAt(len - 1);
  char beforeLastChar = sb.charAt(len - 2);
  sb.setCharAt(len - 1, beforeLastChar);
  sb.setCharAt(len - 2, lastChar);
}
return sb.toString();
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40517102
public String lastTwo(String str) {
int len=str.length();

StringBuilder sb=new StringBuilder(str);
if(len>=2){
char lastChar=sb.charAt(len-1);
char beforeLastChar=sb.charAt(len-2);
sb.setChar(len-1,beforeLastChar);
sb.setChar(len-2,lastChar);


  }
    return sb.toString();
  
}

Open in new window


when i tried as above getting below error

Compile problems:


Error:      sb.setChar(len-1,beforeLastChar);
         ^^^^^^^
The method setChar(int, char) is undefined for the type StringBuilder


see Example Code to help with compile problems


please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40517105
not sure how above is different from below working code
public String lastTwo(String str) {
StringBuilder sb = new StringBuilder(str);
int len=sb.length();
if(len>=2){
  char lastChar = sb.charAt(len - 1);
  char beforeLastChar = sb.charAt(len - 2);
  sb.setCharAt(len - 1, beforeLastChar);
  sb.setCharAt(len - 2, lastChar);
}
return sb.toString();
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40517216
setChar is different from setCharAt
0
 
LVL 7

Author Comment

by:gudii9
ID: 40517238
Oh when to use each one. How are they different.  Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40517252
setCharAt is a StringBuilder method that you can use when you want to set the character at a specified index http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#setCharAt%28int,%20char%29

StringBuilder objects do not have a setChar method, so you can never use it on a StringBuilder object
(although you might use setChar on an Array object, or a Field object or some other object that actually has such a method)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40517271
Oh
0
 
LVL 7

Author Comment

by:gudii9
ID: 40518844
setChar() works similar setCharAt() right for other objects like array etc(non string builder objects).
Please advise
0
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 40518856
Yes, they do work similarly for different objects, so you could rewrite your routine to use an Array instead of a StringBuilder, and then use setChar() instead of setCharAt()
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
A short article about problems I had with the new location API and permissions in Marshmallow
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now