gudii9
asked on
string characters first three multiple times
Hi,
I was trying below challenge
http://codingbat.com/prob/p136351
I wrote as below
I get error as below
Error: String str2=str.charAt(0)+str.cha rAt(1)+str .charAt(2) ;
^^^^
Type mismatch: cannot convert from int to String
what is the effective way to solve this challenge. Please advise. Thanks in advance
I was trying below challenge
http://codingbat.com/prob/p136351
I wrote as below
public String front3(String str) {
if(str.length()==3){
return str+str+str;
}
if(str.length()>3){
String str2=str.charAt(0)+str.charAt(1)+str .charAt(2) ;
return str2+str2+str2;
}
}
I get error as below
Error: String str2=str.charAt(0)+str.cha
^^^^
Type mismatch: cannot convert from int to String
what is the effective way to solve this challenge. Please advise. Thanks in advance
ASKER
I changed as below
i still get error as
Compile problems:
Error: public String front3(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.
Please advise
public String front3(String str) {
if(str.length()==3){
return str+str+str;
}
if(str.length()>3){
String str2=new String(new char[]{str.charAt(0),str.charAt(1),str.charAt(2)});
return str2;
}
}
i still get error as
Compile problems:
Error: public String front3(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.
Please advise
What is the String that you are passing to the method?
there may be another possibility besides ==3 and >3
This passes the tests.
public String front3(String str) {
if(str.length()<=3){
return str+str+str;
}
else{
String str2 = "" + str.charAt(0) + str.charAt(1) + str.charAt(2);
return str2+str2+str2;
}
}
I just used the empty String ("") to force Java to make a String out of the concatenation of chars.
Here's another way to solve it that is perhaps a little cleaner?
Math.min(3,str.length()) will be either 3 or the length of the string - whichever is smaller.
Another general rule for writing clean code is to avoid "if () ... else ..." whenever possible, because it makes the flow of the code a little harder to understand.
Doug
Math.min(3,str.length()) will be either 3 or the length of the string - whichever is smaller.
public String front3(String str) {
String front = str.substring(0,Math.min(3,str.length())) ;
return front + front + front ;
}
Another general rule for writing clean code is to avoid "if () ... else ..." whenever possible, because it makes the flow of the code a little harder to understand.
Doug
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could always squeeze a bit more abstraction out of the situation, by this, and again pass some burden back to Java :
(Meaning you'd 'return' sA, for later decomposition*).
String s = "hello";
final int FRONT = Math.min(s.length(),3);
String[] sA = new String[FRONT];
Arrays.fill(sA,s.substring(0,sA.length));
for(int r=0;r<sA.length;r++){System.out.print(sA[r]);}//*
(Meaning you'd 'return' sA, for later decomposition*).
avoid "if () ... else ..." whenever possibleTo avoid "if () ... else ..." is always possible, but what you may end up using instead
could make the flow of the code harder to understand.
To always avoid "if () ... else ..." suggests the sort of general rule up with which I will not put.
avoid "if () ... else ..." whenever possibleI don't agree. It is perfectly acceptable when the scope of the else is clear. In this case, the length of the String is <=3 or its not.
So I suspect that dpearson didn't really mean "whenever possible"
(or perhaps didn't really mean "if () ... else ..."?)
"when not called for" would be something I'd agree with, but it would also seem unnecessary to say.
(or perhaps didn't really mean "if () ... else ..."?)
"when not called for" would be something I'd agree with, but it would also seem unnecessary to say.
If I had to take only one tool with me to my programmers' desert island, it'd be the 'if' statement. ;)
(or perhaps didn't really mean "if () ... else ..."?)
I suspect he meant 'if () ... if() ...' etc.
Hmm . . . that sounds a bit iffy.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not all of those points are specific to "if () ... else ..." per se, but "when it's reasonable" does seem a better guideline to me than "whenever possible"
Doug, you have convinced me. I agree with you. Thank you for sharing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public String front3(String str) {
if(str.length()==3){
return str+str+str;
}
if(str.length()>3){
String str2=str.charAt(0)+str.charAt(1)+str.charAt(2);
return str2+str2+str2;
}
}
why i got error as in original post
if i write like below
public String front3(String str) {
if(str.length()==3){
return str+str+str;
}
if(str.length()>3){
String str2=str.charAt(0)+str.charAt(1)+str.charAt(2);
return str2+str2+str2;
}
return null;
}
But not sure why to add null at the method last line since method expects String. That seems not correct approach of returning null.
What heppens if i pass illegal out of range 'n' who handle it. please advise
What heppens if i pass illegal out of range 'n' who handle it.Are you talking about your other question at
https://www.experts-exchange.com/questions/28525201/removing-a-character-from-string.html
? It doesn't apply here.
But not sure why to add null at the method last line since method expects String. That seems not correct approach of returning null.Returning null is ok. The uninitialized state of String is null. It might be better to return a String such as "Error".
why i got error as in original postThe compiler insists that you return a value in every path through your code. If the two conditionals fail, then there would be a third path and you have to return a value.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public String front3(String str) {
if(str.length()<=3){
return str+str+str;
}
else{
String str2 = "" + str.charAt(0) + str.charAt(1) + str.charAt(2);
return str2+str2+str2;
}
}
I just used the empty String ("") to force Java to make a String out of the concatenation of chars
If i give as below concatenation does not happen?(as i only have three characters not strings)
public String front3(String str) {
if(str.length()<=3){
return str+str+str;
}
else{
String str2 = str.charAt(0) + str.charAt(1) + str.charAt(2);
return str2+str2+str2;
}
}
Without adding the empty String the chars will be interpreted as ints.
Without adding the empty String the chars will be interpreted as ints.
Maybe not quite 'interpreted', as it of course it won't even compile.
ASKER
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test2 t=new Test2();
t.front3("cat");
}
public String front3(String str){
if(str.length()<=3){
return str+str+str;
}
else{
String str2 = "" + str.charAt(0) + str.charAt(1) + str.charAt(2);
return str2+str2+str2;
}
}
I see when i remove "" i got compilation error.
But when i run above java application i do not see any console output or test green or anything like that. How do i make sure it is working in eclipse as well not just in the codingbat site. please advise
But when i run above java application i do not see any console output
Why would you? You haven't output anything to the console
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
now i see console output
catcatcat
so basically "" means nothing. Just a trick to fool java so that it thinks as string(not int) other than that it is not adding any value in above post example right? It is not even adding one space either.
catcatcat
so basically "" means nothing. Just a trick to fool java so that it thinks as string(not int) other than that it is not adding any value in above post example right? It is not even adding one space either.
not adding any value in above post example right? It is not even adding one space either.Yes.
There are many ways to create a String from a char. Here are some.
new String(new char[]{str.charAt(0)});
String.valueOf(str.charAt(
Character.toString(str.cha
"" + str.charAt(0)
Open in new window