Similar code, yet one version does not return a String when it should












3














For a class exercise, both my friend and I wrote a similar code. Hers works while mine gives out:



Error: This method must return a result of type java.lang.String



Her code :



 public static String alphaString (String a, String b) {

for (int i=0; i<a.length(); i++){
if ((int)(a.charAt(i)) < ((int)(b.charAt(i)))) {
return a;
}
if ((int)(a.charAt(i)) > ((int)(b.charAt(i)))) {
return b;
}
}

return ("these are the same words") ;

}


My code :



// method that takes two Strings and returns the one that comes first in the alphabet



// alphaString("banana", "apple")--> "apple"



// alphaString("snake", "squirrel")--> "snake".



public static String alphaString(String s1, String s2) {


for (int i = 0; i<s1.length(); i++ ) {
if (s1.charAt(i)<s2.charAt(i)) {
return s1;
}
if (s1.charAt(i)>s2.charAt(i)){
return s2;

}


}


Now, I can tell that our variables are named differently and that she uses (int) (though I don't think it is necessary). I don't understand why her code works and mine not. If I try to also add the (int) and make the code even more similar, I still get the error.



Does anyone have any idea why that might be?










share|improve this question
























  • What is the last statement of her method? What is the last statement of your method? What is the difference?
    – camickr
    Nov 23 '18 at 2:21












  • What would her method return if the first string was empty (length 0)? What would yours return?
    – David
    Nov 23 '18 at 2:21










  • @David mine wouldn't return anything at all because of the error, while hers would return "these are the same words"
    – Patricia Manarazan
    Nov 23 '18 at 2:24






  • 1




    @PatriciaManarazan: "mine wouldn't return anything at all" - That is the error. The compiler has to ensure that the method will always return the advertised type. There exists a logical condition where your method wouldn't return anything, which is invalid.
    – David
    Nov 23 '18 at 2:25












  • @camickr hers: return ("these are the same words") ; mine: return s2;
    – Patricia Manarazan
    Nov 23 '18 at 2:26
















3














For a class exercise, both my friend and I wrote a similar code. Hers works while mine gives out:



Error: This method must return a result of type java.lang.String



Her code :



 public static String alphaString (String a, String b) {

for (int i=0; i<a.length(); i++){
if ((int)(a.charAt(i)) < ((int)(b.charAt(i)))) {
return a;
}
if ((int)(a.charAt(i)) > ((int)(b.charAt(i)))) {
return b;
}
}

return ("these are the same words") ;

}


My code :



// method that takes two Strings and returns the one that comes first in the alphabet



// alphaString("banana", "apple")--> "apple"



// alphaString("snake", "squirrel")--> "snake".



public static String alphaString(String s1, String s2) {


for (int i = 0; i<s1.length(); i++ ) {
if (s1.charAt(i)<s2.charAt(i)) {
return s1;
}
if (s1.charAt(i)>s2.charAt(i)){
return s2;

}


}


Now, I can tell that our variables are named differently and that she uses (int) (though I don't think it is necessary). I don't understand why her code works and mine not. If I try to also add the (int) and make the code even more similar, I still get the error.



Does anyone have any idea why that might be?










share|improve this question
























  • What is the last statement of her method? What is the last statement of your method? What is the difference?
    – camickr
    Nov 23 '18 at 2:21












  • What would her method return if the first string was empty (length 0)? What would yours return?
    – David
    Nov 23 '18 at 2:21










  • @David mine wouldn't return anything at all because of the error, while hers would return "these are the same words"
    – Patricia Manarazan
    Nov 23 '18 at 2:24






  • 1




    @PatriciaManarazan: "mine wouldn't return anything at all" - That is the error. The compiler has to ensure that the method will always return the advertised type. There exists a logical condition where your method wouldn't return anything, which is invalid.
    – David
    Nov 23 '18 at 2:25












  • @camickr hers: return ("these are the same words") ; mine: return s2;
    – Patricia Manarazan
    Nov 23 '18 at 2:26














3












3








3







For a class exercise, both my friend and I wrote a similar code. Hers works while mine gives out:



Error: This method must return a result of type java.lang.String



Her code :



 public static String alphaString (String a, String b) {

for (int i=0; i<a.length(); i++){
if ((int)(a.charAt(i)) < ((int)(b.charAt(i)))) {
return a;
}
if ((int)(a.charAt(i)) > ((int)(b.charAt(i)))) {
return b;
}
}

return ("these are the same words") ;

}


My code :



// method that takes two Strings and returns the one that comes first in the alphabet



// alphaString("banana", "apple")--> "apple"



// alphaString("snake", "squirrel")--> "snake".



public static String alphaString(String s1, String s2) {


for (int i = 0; i<s1.length(); i++ ) {
if (s1.charAt(i)<s2.charAt(i)) {
return s1;
}
if (s1.charAt(i)>s2.charAt(i)){
return s2;

}


}


Now, I can tell that our variables are named differently and that she uses (int) (though I don't think it is necessary). I don't understand why her code works and mine not. If I try to also add the (int) and make the code even more similar, I still get the error.



Does anyone have any idea why that might be?










share|improve this question















For a class exercise, both my friend and I wrote a similar code. Hers works while mine gives out:



Error: This method must return a result of type java.lang.String



Her code :



 public static String alphaString (String a, String b) {

for (int i=0; i<a.length(); i++){
if ((int)(a.charAt(i)) < ((int)(b.charAt(i)))) {
return a;
}
if ((int)(a.charAt(i)) > ((int)(b.charAt(i)))) {
return b;
}
}

return ("these are the same words") ;

}


My code :



// method that takes two Strings and returns the one that comes first in the alphabet



// alphaString("banana", "apple")--> "apple"



// alphaString("snake", "squirrel")--> "snake".



public static String alphaString(String s1, String s2) {


for (int i = 0; i<s1.length(); i++ ) {
if (s1.charAt(i)<s2.charAt(i)) {
return s1;
}
if (s1.charAt(i)>s2.charAt(i)){
return s2;

}


}


Now, I can tell that our variables are named differently and that she uses (int) (though I don't think it is necessary). I don't understand why her code works and mine not. If I try to also add the (int) and make the code even more similar, I still get the error.



Does anyone have any idea why that might be?







java string for-loop types alphabetical






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 2:20









camickr

274k15126239




274k15126239










asked Nov 23 '18 at 2:18









Patricia Manarazan

206




206












  • What is the last statement of her method? What is the last statement of your method? What is the difference?
    – camickr
    Nov 23 '18 at 2:21












  • What would her method return if the first string was empty (length 0)? What would yours return?
    – David
    Nov 23 '18 at 2:21










  • @David mine wouldn't return anything at all because of the error, while hers would return "these are the same words"
    – Patricia Manarazan
    Nov 23 '18 at 2:24






  • 1




    @PatriciaManarazan: "mine wouldn't return anything at all" - That is the error. The compiler has to ensure that the method will always return the advertised type. There exists a logical condition where your method wouldn't return anything, which is invalid.
    – David
    Nov 23 '18 at 2:25












  • @camickr hers: return ("these are the same words") ; mine: return s2;
    – Patricia Manarazan
    Nov 23 '18 at 2:26


















  • What is the last statement of her method? What is the last statement of your method? What is the difference?
    – camickr
    Nov 23 '18 at 2:21












  • What would her method return if the first string was empty (length 0)? What would yours return?
    – David
    Nov 23 '18 at 2:21










  • @David mine wouldn't return anything at all because of the error, while hers would return "these are the same words"
    – Patricia Manarazan
    Nov 23 '18 at 2:24






  • 1




    @PatriciaManarazan: "mine wouldn't return anything at all" - That is the error. The compiler has to ensure that the method will always return the advertised type. There exists a logical condition where your method wouldn't return anything, which is invalid.
    – David
    Nov 23 '18 at 2:25












  • @camickr hers: return ("these are the same words") ; mine: return s2;
    – Patricia Manarazan
    Nov 23 '18 at 2:26
















What is the last statement of her method? What is the last statement of your method? What is the difference?
– camickr
Nov 23 '18 at 2:21






What is the last statement of her method? What is the last statement of your method? What is the difference?
– camickr
Nov 23 '18 at 2:21














What would her method return if the first string was empty (length 0)? What would yours return?
– David
Nov 23 '18 at 2:21




What would her method return if the first string was empty (length 0)? What would yours return?
– David
Nov 23 '18 at 2:21












@David mine wouldn't return anything at all because of the error, while hers would return "these are the same words"
– Patricia Manarazan
Nov 23 '18 at 2:24




@David mine wouldn't return anything at all because of the error, while hers would return "these are the same words"
– Patricia Manarazan
Nov 23 '18 at 2:24




1




1




@PatriciaManarazan: "mine wouldn't return anything at all" - That is the error. The compiler has to ensure that the method will always return the advertised type. There exists a logical condition where your method wouldn't return anything, which is invalid.
– David
Nov 23 '18 at 2:25






@PatriciaManarazan: "mine wouldn't return anything at all" - That is the error. The compiler has to ensure that the method will always return the advertised type. There exists a logical condition where your method wouldn't return anything, which is invalid.
– David
Nov 23 '18 at 2:25














@camickr hers: return ("these are the same words") ; mine: return s2;
– Patricia Manarazan
Nov 23 '18 at 2:26




@camickr hers: return ("these are the same words") ; mine: return s2;
– Patricia Manarazan
Nov 23 '18 at 2:26












1 Answer
1






active

oldest

votes


















3














You seem to be missing the default return of "these are the same words". It isn't clear why you both chose to implement this with a loop (or why neither of you chose to use an else). Regardless, String is Comparable so I would simply do



public static String alphaString(String s1, String s2) {
int c = s1.compareTo(s2);
if (c < 0) {
return s1;
} else if (c > 0) {
return s2;
}
return "these are the same words";
}





share|improve this answer





















  • We used a loop because that was the direction our teacher gave us and we have not seen s1.compareTo(s2) formally in class, so we were not allowed.
    – Patricia Manarazan
    Nov 23 '18 at 2:29










  • When I add : return "these are the same words"; to my original method I can make it compile and It does return a String but I don't understand why. How does that statement affect the rest? why does its absence cause the error?
    – Patricia Manarazan
    Nov 23 '18 at 2:46






  • 1




    The method advertises that it returns a String, therefore, every possible exit out of the method must return a String. In the case where both of the if conditions evaluate to false, there is not a return statement. Therefore, there is a possible exit out of the method that does not return a String, which is the reason for the compile error.
    – John Camerin
    Nov 23 '18 at 2:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439919%2fsimilar-code-yet-one-version-does-not-return-a-string-when-it-should%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You seem to be missing the default return of "these are the same words". It isn't clear why you both chose to implement this with a loop (or why neither of you chose to use an else). Regardless, String is Comparable so I would simply do



public static String alphaString(String s1, String s2) {
int c = s1.compareTo(s2);
if (c < 0) {
return s1;
} else if (c > 0) {
return s2;
}
return "these are the same words";
}





share|improve this answer





















  • We used a loop because that was the direction our teacher gave us and we have not seen s1.compareTo(s2) formally in class, so we were not allowed.
    – Patricia Manarazan
    Nov 23 '18 at 2:29










  • When I add : return "these are the same words"; to my original method I can make it compile and It does return a String but I don't understand why. How does that statement affect the rest? why does its absence cause the error?
    – Patricia Manarazan
    Nov 23 '18 at 2:46






  • 1




    The method advertises that it returns a String, therefore, every possible exit out of the method must return a String. In the case where both of the if conditions evaluate to false, there is not a return statement. Therefore, there is a possible exit out of the method that does not return a String, which is the reason for the compile error.
    – John Camerin
    Nov 23 '18 at 2:56
















3














You seem to be missing the default return of "these are the same words". It isn't clear why you both chose to implement this with a loop (or why neither of you chose to use an else). Regardless, String is Comparable so I would simply do



public static String alphaString(String s1, String s2) {
int c = s1.compareTo(s2);
if (c < 0) {
return s1;
} else if (c > 0) {
return s2;
}
return "these are the same words";
}





share|improve this answer





















  • We used a loop because that was the direction our teacher gave us and we have not seen s1.compareTo(s2) formally in class, so we were not allowed.
    – Patricia Manarazan
    Nov 23 '18 at 2:29










  • When I add : return "these are the same words"; to my original method I can make it compile and It does return a String but I don't understand why. How does that statement affect the rest? why does its absence cause the error?
    – Patricia Manarazan
    Nov 23 '18 at 2:46






  • 1




    The method advertises that it returns a String, therefore, every possible exit out of the method must return a String. In the case where both of the if conditions evaluate to false, there is not a return statement. Therefore, there is a possible exit out of the method that does not return a String, which is the reason for the compile error.
    – John Camerin
    Nov 23 '18 at 2:56














3












3








3






You seem to be missing the default return of "these are the same words". It isn't clear why you both chose to implement this with a loop (or why neither of you chose to use an else). Regardless, String is Comparable so I would simply do



public static String alphaString(String s1, String s2) {
int c = s1.compareTo(s2);
if (c < 0) {
return s1;
} else if (c > 0) {
return s2;
}
return "these are the same words";
}





share|improve this answer












You seem to be missing the default return of "these are the same words". It isn't clear why you both chose to implement this with a loop (or why neither of you chose to use an else). Regardless, String is Comparable so I would simply do



public static String alphaString(String s1, String s2) {
int c = s1.compareTo(s2);
if (c < 0) {
return s1;
} else if (c > 0) {
return s2;
}
return "these are the same words";
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 2:25









Elliott Frisch

152k1389178




152k1389178












  • We used a loop because that was the direction our teacher gave us and we have not seen s1.compareTo(s2) formally in class, so we were not allowed.
    – Patricia Manarazan
    Nov 23 '18 at 2:29










  • When I add : return "these are the same words"; to my original method I can make it compile and It does return a String but I don't understand why. How does that statement affect the rest? why does its absence cause the error?
    – Patricia Manarazan
    Nov 23 '18 at 2:46






  • 1




    The method advertises that it returns a String, therefore, every possible exit out of the method must return a String. In the case where both of the if conditions evaluate to false, there is not a return statement. Therefore, there is a possible exit out of the method that does not return a String, which is the reason for the compile error.
    – John Camerin
    Nov 23 '18 at 2:56


















  • We used a loop because that was the direction our teacher gave us and we have not seen s1.compareTo(s2) formally in class, so we were not allowed.
    – Patricia Manarazan
    Nov 23 '18 at 2:29










  • When I add : return "these are the same words"; to my original method I can make it compile and It does return a String but I don't understand why. How does that statement affect the rest? why does its absence cause the error?
    – Patricia Manarazan
    Nov 23 '18 at 2:46






  • 1




    The method advertises that it returns a String, therefore, every possible exit out of the method must return a String. In the case where both of the if conditions evaluate to false, there is not a return statement. Therefore, there is a possible exit out of the method that does not return a String, which is the reason for the compile error.
    – John Camerin
    Nov 23 '18 at 2:56
















We used a loop because that was the direction our teacher gave us and we have not seen s1.compareTo(s2) formally in class, so we were not allowed.
– Patricia Manarazan
Nov 23 '18 at 2:29




We used a loop because that was the direction our teacher gave us and we have not seen s1.compareTo(s2) formally in class, so we were not allowed.
– Patricia Manarazan
Nov 23 '18 at 2:29












When I add : return "these are the same words"; to my original method I can make it compile and It does return a String but I don't understand why. How does that statement affect the rest? why does its absence cause the error?
– Patricia Manarazan
Nov 23 '18 at 2:46




When I add : return "these are the same words"; to my original method I can make it compile and It does return a String but I don't understand why. How does that statement affect the rest? why does its absence cause the error?
– Patricia Manarazan
Nov 23 '18 at 2:46




1




1




The method advertises that it returns a String, therefore, every possible exit out of the method must return a String. In the case where both of the if conditions evaluate to false, there is not a return statement. Therefore, there is a possible exit out of the method that does not return a String, which is the reason for the compile error.
– John Camerin
Nov 23 '18 at 2:56




The method advertises that it returns a String, therefore, every possible exit out of the method must return a String. In the case where both of the if conditions evaluate to false, there is not a return statement. Therefore, there is a possible exit out of the method that does not return a String, which is the reason for the compile error.
– John Camerin
Nov 23 '18 at 2:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439919%2fsimilar-code-yet-one-version-does-not-return-a-string-when-it-should%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo