Solved

lastTwo challenge

Posted on 2014-12-15
18
84 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
LVL 32

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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