true/false

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Energizer100
    New Member
    • Nov 2007
    • 60

    true/false

    Hey guys.

    I'm having trouble returning true and false things in java.

    [code=java]
    import java.io.*;

    public class StringUtil
    {
    public static void main(String args[]) throws IOException
    {
    BufferedReader reverseString = new BufferedReader( new InputStreamRead er(System.in));
    String revString;
    BufferedReader palindromeStrin g = new BufferedReader( new InputStreamRead er(System.in));
    String palString;
    BufferedReader pigLatinString = new BufferedReader( new InputStreamRead er(System.in));
    String plString;
    BufferedReader shortHandString = new BufferedReader( new InputStreamRead er(System.in));
    String shString;
    int i = 0;
    int indexPalString = 0;

    System.out.prin tln("Enter a potential Palindrome:");
    palString = palindromeStrin g.readLine();
    indexPalString = palString.lengt h() - 1;

    for(i = 0; i <= indexPalString; i++)
    {
    if (palString.char At(i) == palString.charA t(indexPalStrin g - i))
    {
    return true;
    }
    else{
    return false;
    }
    }
    }
    }
    [/code]


    I want it to check whether or not it is a palindrome. It HAS to return true or false.
    And is there anyway that i could ignore case, spaces, and punctuation?

    Thank You
    Last edited by JosAH; Nov 17 '07, 10:09 PM. Reason: added [code] ...[/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Two small questions to you:

    1) What are all those BufferedReaders doing there?
    2) your main method is supposed to return nothing (a void); why HAS it supposed
    to return a boolean value?

    kind regards,

    Jos

    Comment

    • Energizer100
      New Member
      • Nov 2007
      • 60

      #3
      Originally posted by JosAH
      Two small questions to you:

      1) What are all those BufferedReaders doing there?
      2) your main method is supposed to return nothing (a void); why HAS it supposed
      to return a boolean value?

      kind regards,

      Jos
      The buffered readers are there because that's what i used in one of my other program for inputted characters.

      So should i put all of the calculations before the main method?

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        From the sounds of it, you either need to write a function to check for palindromes that would return a boolean, and call it in your main, or print whether or not it is a palindrome in your main rather than trying to return something to a void function.

        Comment

        • Energizer100
          New Member
          • Nov 2007
          • 60

          #5
          Originally posted by Laharl
          From the sounds of it, you either need to write a function to check for palindromes that would return a boolean, and call it in your main, or print whether or not it is a palindrome in your main rather than trying to return something to a void function.
          import java.util.Scann er;
          import java.io.*;

          public class StringUtil
          {
          public static void main(String args[])
          {
          Scanner reverseString = new Scanner(System. in);
          String revString;
          Scanner palindromeStrin g = new Scanner(System. in));
          String palString;
          Scanner pigLatinString = new Scanner(System. in));
          String plString;
          Scanner shortHandString = new Scanner(System. in));
          String shString;
          int i = 0;
          int indexPalString = 0;

          System.out.prin tln("Enter a potential Palindrome:");
          palString = palindromeStrin g.readLine();
          indexPalString = palString.lengt h() - 1;

          for(i = 0; i <= indexPalString; i++)
          {
          if (palString.char At(i).equals(pa lString.charAt( indexPalString - i)))
          {
          System.out.prin tln("True");
          }
          else{
          System.out.prin tln("False");
          }
          }


          }
          }


          When i run it, it says true/false for every letter. how do i stop that?

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Move one of those printing statements outside the loop, but leave the other where it is, and use a boolean to cover whether or not it is a palindrome. You can use the break; keyword to leave the loop at any time, assuming it works the same as in C++.

            Eg:
            [CODE=Java] \\Code that checks if two strings are equal, and prints out if they are-assumes they have the same length
            String s = "Hi";
            String t = "Bi";
            for (int a = 0; a < s.length(); a++){
            if (s.charAt(a) != t.charAt(a)){
            System.out.prin tln(s+" and " +t+" are not the same.");
            break;
            }
            }
            if (a == s.length())
            System.out.prin tln("These are the same.");
            [/CODE]

            Can you extrapolate the code you need from here?

            Comment

            • Energizer100
              New Member
              • Nov 2007
              • 60

              #7
              Originally posted by Laharl
              Move one of those printing statements outside the loop, but leave the other where it is, and use a boolean to cover whether or not it is a palindrome. You can use the break; keyword to leave the loop at any time, assuming it works the same as in C++.

              Eg:
              [CODE=Java] \\Code that checks if two strings are equal, and prints out if they are-assumes they have the same length
              String s = "Hi";
              String t = "Bi";
              for (int a = 0; a < s.length(); a++){
              if (s.charAt(a) != t.charAt(a)){
              System.out.prin tln(s+" and " +t+" are not the same.");
              break;
              }
              }
              if (a == s.length())
              System.out.prin tln("These are the same.");
              [/CODE]

              Can you extrapolate the code you need from here?
              hmmm. ill try adding the break statement, and see if it works

              It does.

              import java.util.Scann er;
              import java.io.*;

              public class StringUtil
              {
              public static void main(String args[]) throws IOException
              {
              BufferedReader reverseString = new BufferedReader( new InputStreamRead er(System.in));
              String revString;
              BufferedReader palindromeStrin g = new BufferedReader( new InputStreamRead er(System.in));
              String palString;
              BufferedReader pigLatinString = new BufferedReader( new InputStreamRead er(System.in));
              String plString;
              BufferedReader shortHandString = new BufferedReader( new InputStreamRead er(System.in));
              String shString;
              int i = 0;
              int indexPalString = 0;

              System.out.prin tln("Enter a potential Palindrome:");
              palString = palindromeStrin g.readLine();
              indexPalString = palString.lengt h() - 1;

              for(i = 0; i <= indexPalString; i++)
              {
              if (palString.char At(i) != palString.charA t(indexPalStrin g - i))
              {
              System.out.prin tln("False");
              break;
              }
              else{
              System.out.prin tln("True");
              break;
              }
              }


              }
              }


              Where do i put the ignore case stuff? And is there a way to ignore punctuation and spaces? So if someone were to write Race Car, it would return true.

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                You're very close, but your current code will only check the first character against the last and then break the loop because either they are equal, in which case you print that out and break, or they aren't, in which case you print that and break. You only want to break the loop in one of those two cases and can't deal with the other case until after the loop, so you'll need to move your print statements around. Can you see how to do that?

                Comment

                • Energizer100
                  New Member
                  • Nov 2007
                  • 60

                  #9
                  Originally posted by Laharl
                  You're very close, but your current code will only check the first character against the last and then break the loop because either they are equal, in which case you print that out and break, or they aren't, in which case you print that and break. You only want to break the loop in one of those two cases and can't deal with the other case until after the loop, so you'll need to move your print statements around. Can you see how to do that?

                  I don't know how to do that. I tried moving the break around and nothing happened. But i don't know why i have to move the output statements around

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    You have to move the True print statement outside the loop because you can't conclude true unles it clears the loop without breaking prematurely. Can you see how to check whether or not you've got that case (and thus need to output True)? As a hint, the example code I gave you earlier is close to what you need.

                    Comment

                    • Energizer100
                      New Member
                      • Nov 2007
                      • 60

                      #11
                      Originally posted by Laharl
                      You have to move the True print statement outside the loop because you can't conclude true unles it clears the loop without breaking prematurely. Can you see how to check whether or not you've got that case (and thus need to output True)? As a hint, the example code I gave you earlier is close to what you need.
                      import java.util.Scann er;
                      import java.io.*;

                      public class StringUtil
                      {
                      public static void main(String args[]) throws IOException
                      {
                      BufferedReader reverseString = new BufferedReader( new InputStreamRead er(System.in));
                      String revString;
                      BufferedReader palindromeStrin g = new BufferedReader( new InputStreamRead er(System.in));
                      String palString;
                      BufferedReader pigLatinString = new BufferedReader( new InputStreamRead er(System.in));
                      String plString;
                      BufferedReader shortHandString = new BufferedReader( new InputStreamRead er(System.in));
                      String shString;
                      int i = 0;
                      int indexPalString = 0;

                      System.out.prin tln("Enter a potential Palindrome:");
                      palString = palindromeStrin g.readLine();
                      indexPalString = palString.lengt h() - 1;

                      for(i = 0; i <= indexPalString; i++)
                      {
                      if (palString.char At(i) != palString.charA t(indexPalStrin g - i))
                      {
                      System.out.prin tln("False");
                      break;
                      }
                      }
                      if (palString.char At(i) == palString.charA t(indexPalStrin g - 1))
                      System.out.prin tln("True");
                      }
                      }


                      When i run it, there is an error message for something like "bob". I reviewed the code again and I don't know what's wrong

                      Comment

                      • Energizer100
                        New Member
                        • Nov 2007
                        • 60

                        #12
                        Originally posted by Energizer100
                        import java.util.Scann er;
                        import java.io.*;

                        public class StringUtil
                        {
                        public static void main(String args[]) throws IOException
                        {
                        BufferedReader reverseString = new BufferedReader( new InputStreamRead er(System.in));
                        String revString;
                        BufferedReader palindromeStrin g = new BufferedReader( new InputStreamRead er(System.in));
                        String palString;
                        BufferedReader pigLatinString = new BufferedReader( new InputStreamRead er(System.in));
                        String plString;
                        BufferedReader shortHandString = new BufferedReader( new InputStreamRead er(System.in));
                        String shString;
                        int i = 0;
                        int indexPalString = 0;

                        System.out.prin tln("Enter a potential Palindrome:");
                        palString = palindromeStrin g.readLine();
                        indexPalString = palString.lengt h() - 1;

                        for(i = 0; i <= indexPalString; i++)
                        {
                        if (palString.char At(i) != palString.charA t(indexPalStrin g - i))
                        {
                        System.out.prin tln("False");
                        break;
                        }
                        }
                        if (palString.char At(i) == palString.charA t(indexPalStrin g - 1))
                        System.out.prin tln("True");
                        }
                        }


                        When i run it, there is an error message for something like "bob". I reviewed the code again and I don't know what's wrong

                        Never mind, i got it

                        Comment

                        Working...