Why is my method returning false even though the conditions are true?How can I “delimit” an integer from a given string?Reverse array algorithmMethod override returns nullJava - Method executed prior to Default ConstructorHow can i insert multiple input in one line in 2dArray loopingI/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?How can I print a pyramid of *Why does my method say it must return a type string and gives me an error even though my method does return a type string
Do high-wing aircraft represent more difficult engineering challenges than low-wing aircraft?
Is my test coverage up to snuff?
Holding rent money for my friend which amounts to over $10k?
How to describe a building set which is like LEGO without using the "LEGO" word?
Why can't I share a one use code with anyone else?
How to continually let my readers know what time it is in my story, in an organic way?
Geometric inspiration behind Hal's Wolf
I recently started my machine learning PhD and I have absolutely no idea what I'm doing
Using chord iii in a chord progression (major key)
Is random forest for regression a 'true' regression?
Were any of the books mentioned in this scene from the movie Hackers real?
Could a space colony 1g from the sun work?
Why would someone open a Netflix account using my Gmail address?
Understanding Deutch's Algorithm
How to rename multiple files in a directory at the same time
Assembly writer vs compiler
Capital gains on stocks sold to take initial investment off the table
Single word that parallels "Recent" when discussing the near future
What color to choose as "danger" if the main color of my app is red
How to redirect stdout to a file, and stdout+stderr to another one?
Will consteval functions allow template parameters dependent on function arguments?
Does the Rogue's Reliable Talent feature work for thieves' tools, since the rogue is proficient in them?
Why does SSL Labs now consider CBC suites weak?
How might a landlocked lake become a complete ecosystem?
Why is my method returning false even though the conditions are true?
How can I “delimit” an integer from a given string?Reverse array algorithmMethod override returns nullJava - Method executed prior to Default ConstructorHow can i insert multiple input in one line in 2dArray loopingI/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?How can I print a pyramid of *Why does my method say it must return a type string and gives me an error even though my method does return a type string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
public class Lab3Exercises {
public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));
public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;
if (reverseWord.equals(myWord))
return true;
else
return false;
I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false
java
add a comment |
public class Lab3Exercises {
public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));
public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;
if (reverseWord.equals(myWord))
return true;
else
return false;
I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false
java
Does this have anything to the lab utility?
– Sid
Mar 23 at 15:59
For your example word hannah, the value ofreverseWord
after thewhile
loop is hhanna.
– Abra
Mar 23 at 16:31
You can simplify your code,return reverseWord.equals(myWord)
is significantly shorter than that if-else you use...
– Mark Rotteveel
Mar 23 at 16:54
add a comment |
public class Lab3Exercises {
public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));
public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;
if (reverseWord.equals(myWord))
return true;
else
return false;
I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false
java
public class Lab3Exercises {
public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));
public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;
if (reverseWord.equals(myWord))
return true;
else
return false;
I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false
java
java
edited Mar 23 at 15:59
Sid
3,237104085
3,237104085
asked Mar 23 at 15:14
Jessy SiaJessy Sia
853
853
Does this have anything to the lab utility?
– Sid
Mar 23 at 15:59
For your example word hannah, the value ofreverseWord
after thewhile
loop is hhanna.
– Abra
Mar 23 at 16:31
You can simplify your code,return reverseWord.equals(myWord)
is significantly shorter than that if-else you use...
– Mark Rotteveel
Mar 23 at 16:54
add a comment |
Does this have anything to the lab utility?
– Sid
Mar 23 at 15:59
For your example word hannah, the value ofreverseWord
after thewhile
loop is hhanna.
– Abra
Mar 23 at 16:31
You can simplify your code,return reverseWord.equals(myWord)
is significantly shorter than that if-else you use...
– Mark Rotteveel
Mar 23 at 16:54
Does this have anything to the lab utility?
– Sid
Mar 23 at 15:59
Does this have anything to the lab utility?
– Sid
Mar 23 at 15:59
For your example word hannah, the value of
reverseWord
after the while
loop is hhanna.– Abra
Mar 23 at 16:31
For your example word hannah, the value of
reverseWord
after the while
loop is hhanna.– Abra
Mar 23 at 16:31
You can simplify your code,
return reverseWord.equals(myWord)
is significantly shorter than that if-else you use...– Mark Rotteveel
Mar 23 at 16:54
You can simplify your code,
return reverseWord.equals(myWord)
is significantly shorter than that if-else you use...– Mark Rotteveel
Mar 23 at 16:54
add a comment |
3 Answers
3
active
oldest
votes
Let’s start from the simpler examples.
- If
p
has length 1, for examples
(a palindrome), you are settingreverseWord
to the empty string. Sincei
starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal tos
, the method returns false. - Input
tt
(another palindrome). You are running once through the loop, settingreverseWord
to"t" + "t" + ""
="tt"
. The method correctly returns true in this case. - Input
obo
: You are settingreverseWord
first toboo
, then to"oob"
("o" + "ob" + ""
), discarding the first value.
I think you need to think your logic through once more.
Tip: learn to use a debugger.
add a comment |
You're condition isn't true because the way you build the reversed word is wrong.
- You're beginning with
i
equals to0
- You reassign a new value to
reverseWord
in every loop - The way you're building the reversed string is totally a mystery to me
A good solution would be the following (you write the code :))
- Try beginning your loops with the index beginning at the very end of the original word
- Loop down until you reach 0, basically until no letter remains to be appended to
reverseWord
- In every loop, append a single letter to
reverseWord
instead of the result of a complex operation
add a comment |
- No need to make a new reference for the plaindrome word that you're going to check.
- You should store the word in the reverse order in reverseWord variable.
- Last index of a char array (String) in java is 1 less than string length. First index is 0.
So,the while loop read the string from the end to start index and store.
public static boolean palindrome(String p)
String reverseWord = "";
int i = p.length() - 1;
while (i >= 0)
reverseWord += p.charAt(i);
i--;
if (reverseWord.equals(p))
return true;
return false;
1
Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?
– Ole V.V.
Mar 23 at 16:12
Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.
– Harshana
Mar 23 at 16:19
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315192%2fwhy-is-my-method-returning-false-even-though-the-conditions-are-true%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let’s start from the simpler examples.
- If
p
has length 1, for examples
(a palindrome), you are settingreverseWord
to the empty string. Sincei
starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal tos
, the method returns false. - Input
tt
(another palindrome). You are running once through the loop, settingreverseWord
to"t" + "t" + ""
="tt"
. The method correctly returns true in this case. - Input
obo
: You are settingreverseWord
first toboo
, then to"oob"
("o" + "ob" + ""
), discarding the first value.
I think you need to think your logic through once more.
Tip: learn to use a debugger.
add a comment |
Let’s start from the simpler examples.
- If
p
has length 1, for examples
(a palindrome), you are settingreverseWord
to the empty string. Sincei
starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal tos
, the method returns false. - Input
tt
(another palindrome). You are running once through the loop, settingreverseWord
to"t" + "t" + ""
="tt"
. The method correctly returns true in this case. - Input
obo
: You are settingreverseWord
first toboo
, then to"oob"
("o" + "ob" + ""
), discarding the first value.
I think you need to think your logic through once more.
Tip: learn to use a debugger.
add a comment |
Let’s start from the simpler examples.
- If
p
has length 1, for examples
(a palindrome), you are settingreverseWord
to the empty string. Sincei
starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal tos
, the method returns false. - Input
tt
(another palindrome). You are running once through the loop, settingreverseWord
to"t" + "t" + ""
="tt"
. The method correctly returns true in this case. - Input
obo
: You are settingreverseWord
first toboo
, then to"oob"
("o" + "ob" + ""
), discarding the first value.
I think you need to think your logic through once more.
Tip: learn to use a debugger.
Let’s start from the simpler examples.
- If
p
has length 1, for examples
(a palindrome), you are settingreverseWord
to the empty string. Sincei
starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal tos
, the method returns false. - Input
tt
(another palindrome). You are running once through the loop, settingreverseWord
to"t" + "t" + ""
="tt"
. The method correctly returns true in this case. - Input
obo
: You are settingreverseWord
first toboo
, then to"oob"
("o" + "ob" + ""
), discarding the first value.
I think you need to think your logic through once more.
Tip: learn to use a debugger.
answered Mar 23 at 16:18
Ole V.V.Ole V.V.
33.2k74359
33.2k74359
add a comment |
add a comment |
You're condition isn't true because the way you build the reversed word is wrong.
- You're beginning with
i
equals to0
- You reassign a new value to
reverseWord
in every loop - The way you're building the reversed string is totally a mystery to me
A good solution would be the following (you write the code :))
- Try beginning your loops with the index beginning at the very end of the original word
- Loop down until you reach 0, basically until no letter remains to be appended to
reverseWord
- In every loop, append a single letter to
reverseWord
instead of the result of a complex operation
add a comment |
You're condition isn't true because the way you build the reversed word is wrong.
- You're beginning with
i
equals to0
- You reassign a new value to
reverseWord
in every loop - The way you're building the reversed string is totally a mystery to me
A good solution would be the following (you write the code :))
- Try beginning your loops with the index beginning at the very end of the original word
- Loop down until you reach 0, basically until no letter remains to be appended to
reverseWord
- In every loop, append a single letter to
reverseWord
instead of the result of a complex operation
add a comment |
You're condition isn't true because the way you build the reversed word is wrong.
- You're beginning with
i
equals to0
- You reassign a new value to
reverseWord
in every loop - The way you're building the reversed string is totally a mystery to me
A good solution would be the following (you write the code :))
- Try beginning your loops with the index beginning at the very end of the original word
- Loop down until you reach 0, basically until no letter remains to be appended to
reverseWord
- In every loop, append a single letter to
reverseWord
instead of the result of a complex operation
You're condition isn't true because the way you build the reversed word is wrong.
- You're beginning with
i
equals to0
- You reassign a new value to
reverseWord
in every loop - The way you're building the reversed string is totally a mystery to me
A good solution would be the following (you write the code :))
- Try beginning your loops with the index beginning at the very end of the original word
- Loop down until you reach 0, basically until no letter remains to be appended to
reverseWord
- In every loop, append a single letter to
reverseWord
instead of the result of a complex operation
answered Mar 23 at 16:09
Yassin HajajYassin Hajaj
14.8k73062
14.8k73062
add a comment |
add a comment |
- No need to make a new reference for the plaindrome word that you're going to check.
- You should store the word in the reverse order in reverseWord variable.
- Last index of a char array (String) in java is 1 less than string length. First index is 0.
So,the while loop read the string from the end to start index and store.
public static boolean palindrome(String p)
String reverseWord = "";
int i = p.length() - 1;
while (i >= 0)
reverseWord += p.charAt(i);
i--;
if (reverseWord.equals(p))
return true;
return false;
1
Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?
– Ole V.V.
Mar 23 at 16:12
Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.
– Harshana
Mar 23 at 16:19
add a comment |
- No need to make a new reference for the plaindrome word that you're going to check.
- You should store the word in the reverse order in reverseWord variable.
- Last index of a char array (String) in java is 1 less than string length. First index is 0.
So,the while loop read the string from the end to start index and store.
public static boolean palindrome(String p)
String reverseWord = "";
int i = p.length() - 1;
while (i >= 0)
reverseWord += p.charAt(i);
i--;
if (reverseWord.equals(p))
return true;
return false;
1
Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?
– Ole V.V.
Mar 23 at 16:12
Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.
– Harshana
Mar 23 at 16:19
add a comment |
- No need to make a new reference for the plaindrome word that you're going to check.
- You should store the word in the reverse order in reverseWord variable.
- Last index of a char array (String) in java is 1 less than string length. First index is 0.
So,the while loop read the string from the end to start index and store.
public static boolean palindrome(String p)
String reverseWord = "";
int i = p.length() - 1;
while (i >= 0)
reverseWord += p.charAt(i);
i--;
if (reverseWord.equals(p))
return true;
return false;
- No need to make a new reference for the plaindrome word that you're going to check.
- You should store the word in the reverse order in reverseWord variable.
- Last index of a char array (String) in java is 1 less than string length. First index is 0.
So,the while loop read the string from the end to start index and store.
public static boolean palindrome(String p)
String reverseWord = "";
int i = p.length() - 1;
while (i >= 0)
reverseWord += p.charAt(i);
i--;
if (reverseWord.equals(p))
return true;
return false;
edited Mar 23 at 16:39
answered Mar 23 at 16:12
HarshanaHarshana
357212
357212
1
Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?
– Ole V.V.
Mar 23 at 16:12
Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.
– Harshana
Mar 23 at 16:19
add a comment |
1
Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?
– Ole V.V.
Mar 23 at 16:12
Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.
– Harshana
Mar 23 at 16:19
1
1
Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?
– Ole V.V.
Mar 23 at 16:12
Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?
– Ole V.V.
Mar 23 at 16:12
Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.
– Harshana
Mar 23 at 16:19
Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.
– Harshana
Mar 23 at 16:19
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315192%2fwhy-is-my-method-returning-false-even-though-the-conditions-are-true%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Does this have anything to the lab utility?
– Sid
Mar 23 at 15:59
For your example word hannah, the value of
reverseWord
after thewhile
loop is hhanna.– Abra
Mar 23 at 16:31
You can simplify your code,
return reverseWord.equals(myWord)
is significantly shorter than that if-else you use...– Mark Rotteveel
Mar 23 at 16:54