Jump to content

Palindrome *Simplification

- - - - -

  • Please log in to reply
3 replies to this topic

#1
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
So for a lab for my csc course, we had to write a program that checks the dictionary list for palindromes and outputs to a new txt doc. I got everything to work, but just want to know if I can somehow shorten the code.

http://gaia.ecs.csus.../dictionary.txt

   import java.util.Scanner;
   import java.io.*;
   public class Lab12
   {
      public static void main(String[] args)
      {
         Scanner in_obj = null;
         PrintWriter out_obj = null;
         boolean yes;
      
         try
         {
            in_obj = new Scanner(new FileInputStream("dictionary.txt"));
            out_obj = new PrintWriter(new FileOutputStream("pals.txt"));
         } 
            catch (FileNotFoundException e)
            {
               System.out.println("Can't read/write files in folder.");
               System.exit(0);
            }
         
         while (in_obj.hasNextLine())
         {
            String line_read = in_obj.nextLine();
         
            char my_array[] = line_read.toCharArray();
         
            for (int L = 0, R = (my_array.length - 1); L < (my_array.length / 2); L++, R--)
            {
               if (my_array[L] != my_array[R])
               {
                  break;
               }
            
               if (L == my_array.length / 2 - 1)
               {
                  out_obj.println("Hello!  " + line_read);
                  break;
               }
            }
         }
         out_obj.println();
         out_obj.print("Bye, bye!");
         in_obj.close();
         out_obj.close();
       
       
      
      }
   }


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

while (in_obj.hasNextLine()) {

    String line_read = in_obj.nextLine();

    String reverse = new StringBuilder(line_read).reverse().toString();

    if( line_read.equals(reverse) ){

        out_obj.println(line_read);

    }

}

I didn't really test this, but I won't be too far off :)

#3
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
Does that just reverse the current line and checks if they are equal? Seems like an easier way but prof. wanted us to use arrays.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Darn prof...

        String word = "palmlap";

        char[] chars = word.toCharArray();

        boolean palindrome = true;

        for (int i = 0; i < chars.length/2; i++) {

            if(chars[i] != chars[chars.length-i-1]){

                palindrome = false;

                break;

            }

        }

        System.out.println(word + " is " + (palindrome?"": "not ") + "a palindrome");

that's how I'd test with arrays.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users