Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Comparing Strings Reverse if-conditional

Comparing Strings Reverse if-conditional

Scheduled Pinned Locked Moved Java
helpquestionjava
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johtnkucz
    wrote on last edited by
    #1

    the (inputString == reverseString) if-conditional always returns false (even if it is true. please help. Couldn't figure this out. thanks.

    import java.util.Scanner;
    public class ReverseString{
    public static void main (String[] args){
    String inputString="";
    int inputStringLength;
    String reverseString="";
    String stringHolder="";
    //String reverseNumString = "";

    Scanner in = new Scanner(System.in);
    System.out.print("Enter a String: ");
    inputString = in.next();
    inputStringLength = inputString.length();
    System.out.println("Input String is : " +inputString);
    for (int i = 0; i<inputStringLength; i++){
    stringHolder = inputString.charAt(i) + stringHolder;
    reverseString = stringHolder;
    }
    System.out.println(reverseString);
    System.out.println("inputString = " + inputString);
    System.out.println("input length =" + inputString.length() + "reverse length " +reverseString.length());
    System.out.println("Now let's test to see if your input is a palindrome (like hannah or 'a man, a plan, a canal, panama') \\n" + "Does "+ inputString +" = the same forward and reversed? \\n^^^ \\n^^^ " );
    if (inputString == reverseString){
    	System.out.println("yes!");
    	System.out.println(" YES!! Eureka!  " + inputString + " == " + reverseString + "!  Thus, " + inputString + " is a palindrome! gratzee!");
    }else {
    	System.out.println("no");
    	System.out.println("NO!! \\n" + inputString + " != (does not equal) " + reverseString + "\\n So " + inputString + "is not a palindrome!  Oh well no prob!" );
    }
    

    }//end main
    } //end class

    I've tried using scaffolding code to test for what is going on. I think someway reverseString is created may be a problem. But when I print reverseString and inputString they both have the same string length and visibly the same characters...so I don't know what the solution is at present.

    L D 2 Replies Last reply
    0
    • J johtnkucz

      the (inputString == reverseString) if-conditional always returns false (even if it is true. please help. Couldn't figure this out. thanks.

      import java.util.Scanner;
      public class ReverseString{
      public static void main (String[] args){
      String inputString="";
      int inputStringLength;
      String reverseString="";
      String stringHolder="";
      //String reverseNumString = "";

      Scanner in = new Scanner(System.in);
      System.out.print("Enter a String: ");
      inputString = in.next();
      inputStringLength = inputString.length();
      System.out.println("Input String is : " +inputString);
      for (int i = 0; i<inputStringLength; i++){
      stringHolder = inputString.charAt(i) + stringHolder;
      reverseString = stringHolder;
      }
      System.out.println(reverseString);
      System.out.println("inputString = " + inputString);
      System.out.println("input length =" + inputString.length() + "reverse length " +reverseString.length());
      System.out.println("Now let's test to see if your input is a palindrome (like hannah or 'a man, a plan, a canal, panama') \\n" + "Does "+ inputString +" = the same forward and reversed? \\n^^^ \\n^^^ " );
      if (inputString == reverseString){
      	System.out.println("yes!");
      	System.out.println(" YES!! Eureka!  " + inputString + " == " + reverseString + "!  Thus, " + inputString + " is a palindrome! gratzee!");
      }else {
      	System.out.println("no");
      	System.out.println("NO!! \\n" + inputString + " != (does not equal) " + reverseString + "\\n So " + inputString + "is not a palindrome!  Oh well no prob!" );
      }
      

      }//end main
      } //end class

      I've tried using scaffolding code to test for what is going on. I think someway reverseString is created may be a problem. But when I print reverseString and inputString they both have the same string length and visibly the same characters...so I don't know what the solution is at present.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You should be using String.equals()[^].

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      J 1 Reply Last reply
      0
      • L Lost User

        You should be using String.equals()[^].

        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

        J Offline
        J Offline
        johtnkucz
        wrote on last edited by
        #3

        Hey really? Thanks Richard MacCutchan! Thanks for taking the time to look at that code, too. It's not yet solved b/c I have to try that comparison method, but thanks!! However, I thought it was something like that so made a mini test program. String var1 = "hello"; String var2 = "hello"; if (var1==var2){ System.out.println("equal!"); } and THAT did show they were equal. If the String.equals() method works (and gets my ReverseString.java program to work, why would that above var1==var2 work but not so with inputString and reverseString? Thanks!!

        L 1 Reply Last reply
        0
        • J johtnkucz

          Hey really? Thanks Richard MacCutchan! Thanks for taking the time to look at that code, too. It's not yet solved b/c I have to try that comparison method, but thanks!! However, I thought it was something like that so made a mini test program. String var1 = "hello"; String var2 = "hello"; if (var1==var2){ System.out.println("equal!"); } and THAT did show they were equal. If the String.equals() method works (and gets my ReverseString.java program to work, why would that above var1==var2 work but not so with inputString and reverseString? Thanks!!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Well if you had read a bit more detail from the page I pointed you to you would understand that the above expression works only because var1 and var2 refer to the same String object, not because the strings themselves match. And from that it follows that you must always use String.equals() to get an actual comparison of the characters within the strings.

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          J 1 Reply Last reply
          0
          • L Lost User

            Well if you had read a bit more detail from the page I pointed you to you would understand that the above expression works only because var1 and var2 refer to the same String object, not because the strings themselves match. And from that it follows that you must always use String.equals() to get an actual comparison of the characters within the strings.

            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

            J Offline
            J Offline
            johtnkucz
            wrote on last edited by
            #5

            oh bloody hell. was so excited to see a response, didn't even notice that you linked something. I researched and found "The == operator will only be true if two String references point to the same underlying String object. Hence two Strings representing the same content will be equal when tested by the equals(Object) method, but will only by equal when tested with the == operator if they are actually the same object." which I "sorta" understand. like same object in memory referenced and two diff objects but with same string value (== only works as true with the former) so == seems to compare objects in memory and String.equals() actually compares Strings (content of String objects). Sweet am finally getting this a bit!! Thanks for such complete answer...and now that that obstacle is resolved, my coding can progress for the day! (Also I tried it and now that mini-app totally works. Thanks again!) This is site is so much better than stackoverflow haha.

            L 1 Reply Last reply
            0
            • J johtnkucz

              oh bloody hell. was so excited to see a response, didn't even notice that you linked something. I researched and found "The == operator will only be true if two String references point to the same underlying String object. Hence two Strings representing the same content will be equal when tested by the equals(Object) method, but will only by equal when tested with the == operator if they are actually the same object." which I "sorta" understand. like same object in memory referenced and two diff objects but with same string value (== only works as true with the former) so == seems to compare objects in memory and String.equals() actually compares Strings (content of String objects). Sweet am finally getting this a bit!! Thanks for such complete answer...and now that that obstacle is resolved, my coding can progress for the day! (Also I tried it and now that mini-app totally works. Thanks again!) This is site is so much better than stackoverflow haha.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              johtnkucz wrote:

              This is site is so much better than stackoverflow haha.

              If nothing else you get a 5 for that comment. ;) Seriously, thanks for the feedback, it's always welcome, and good luck with your project(s).

              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

              J 1 Reply Last reply
              0
              • L Lost User

                johtnkucz wrote:

                This is site is so much better than stackoverflow haha.

                If nothing else you get a 5 for that comment. ;) Seriously, thanks for the feedback, it's always welcome, and good luck with your project(s).

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                J Offline
                J Offline
                johtnkucz
                wrote on last edited by
                #7

                I never rely on luck. I prefer efficiency and preparation (and utilizing awesome resources like this site!)!! Seriously thanks for the positive feedback...on my positive feedback!! Seriously though This is my number one code-help (I have other resources for tutorials and stuff but if get stuck and need help) this owns other sites, including stack overflow. Your response was extremely helpful. thanks again so much for looking at code. And re stackoverflow. I hate that site. Most comments and posts have this gross disgusting milieu of (ooh look how much coding I know or some cheesy leet rubbish of "no, you're supposed to post this way..or that way.." or whatever. I just was thoroughly stumped, chucked up some code: 1. got the EXACT answer that was the prob 2. learned somethign I never fully understood even after a wretched computer science class and most all autodidactic(== compares objects not content of objects and strings may be equal but diff objects, so if linked to diff objects same content will appear as not equal because their objects are not equal but their content is) . That's the epitome of helpful. Anyways if one gets similar exchanges, this is num 1 code resource. good times. The layout of forum posts/responses is unique and novel but appealing, too. Good tiems. Were you involved in making this site or something, or just prefer this (anti overflow) to other sites, too. Cheers. thanks.

                L 1 Reply Last reply
                0
                • J johtnkucz

                  I never rely on luck. I prefer efficiency and preparation (and utilizing awesome resources like this site!)!! Seriously thanks for the positive feedback...on my positive feedback!! Seriously though This is my number one code-help (I have other resources for tutorials and stuff but if get stuck and need help) this owns other sites, including stack overflow. Your response was extremely helpful. thanks again so much for looking at code. And re stackoverflow. I hate that site. Most comments and posts have this gross disgusting milieu of (ooh look how much coding I know or some cheesy leet rubbish of "no, you're supposed to post this way..or that way.." or whatever. I just was thoroughly stumped, chucked up some code: 1. got the EXACT answer that was the prob 2. learned somethign I never fully understood even after a wretched computer science class and most all autodidactic(== compares objects not content of objects and strings may be equal but diff objects, so if linked to diff objects same content will appear as not equal because their objects are not equal but their content is) . That's the epitome of helpful. Anyways if one gets similar exchanges, this is num 1 code resource. good times. The layout of forum posts/responses is unique and novel but appealing, too. Good tiems. Were you involved in making this site or something, or just prefer this (anti overflow) to other sites, too. Cheers. thanks.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  johtnkucz wrote:

                  Were you involved in making this site or something

                  No, I'm just one of the 8.5 million ... If you want to know a bit more about the site see here[^], and if you want to know who are the great people who did make the site then look here[^].

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  1 Reply Last reply
                  0
                  • J johtnkucz

                    the (inputString == reverseString) if-conditional always returns false (even if it is true. please help. Couldn't figure this out. thanks.

                    import java.util.Scanner;
                    public class ReverseString{
                    public static void main (String[] args){
                    String inputString="";
                    int inputStringLength;
                    String reverseString="";
                    String stringHolder="";
                    //String reverseNumString = "";

                    Scanner in = new Scanner(System.in);
                    System.out.print("Enter a String: ");
                    inputString = in.next();
                    inputStringLength = inputString.length();
                    System.out.println("Input String is : " +inputString);
                    for (int i = 0; i<inputStringLength; i++){
                    stringHolder = inputString.charAt(i) + stringHolder;
                    reverseString = stringHolder;
                    }
                    System.out.println(reverseString);
                    System.out.println("inputString = " + inputString);
                    System.out.println("input length =" + inputString.length() + "reverse length " +reverseString.length());
                    System.out.println("Now let's test to see if your input is a palindrome (like hannah or 'a man, a plan, a canal, panama') \\n" + "Does "+ inputString +" = the same forward and reversed? \\n^^^ \\n^^^ " );
                    if (inputString == reverseString){
                    	System.out.println("yes!");
                    	System.out.println(" YES!! Eureka!  " + inputString + " == " + reverseString + "!  Thus, " + inputString + " is a palindrome! gratzee!");
                    }else {
                    	System.out.println("no");
                    	System.out.println("NO!! \\n" + inputString + " != (does not equal) " + reverseString + "\\n So " + inputString + "is not a palindrome!  Oh well no prob!" );
                    }
                    

                    }//end main
                    } //end class

                    I've tried using scaffolding code to test for what is going on. I think someway reverseString is created may be a problem. But when I print reverseString and inputString they both have the same string length and visibly the same characters...so I don't know what the solution is at present.

                    D Offline
                    D Offline
                    Dinu_6
                    wrote on last edited by
                    #9

                    Buy using stringBuffer and string equals method we can check try this following code

                    import java.util.Scanner;
                    import java.lang.*;
                    public class ReverseString{
                    public static void main (String[] args){
                    String inputString="";
                    int inputStringLength;
                    String reverseString="";
                    String stringHolder="";
                    //String reverseNumString = "";

                    Scanner in = new Scanner(System.in);
                    System.out.print("Enter a String: ");
                    inputString = in.next();
                    StringBuffer sb=new StringBuffer(inputString);
                    sb=sb.reverse();
                    if(inputString.equals(sb.toString()))
                    {
                    	System.out.println("palindrome");
                    }
                    else
                    {
                    	System.out.println("Not palindrome");
                    }
                    

                    }//end main
                    } //end class

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups