Categories
Development

How to identify palindrome in the middle of an array file and print out just the palindrome in a new file

I am trying to create code that will read the file and identify the palindrome that is in the middle of the array of integers (i.e. 5 8 6 11 22 33 44 33 22 11 6 7). I am stuck on how to go about it and the research I’ve been doing online has only solutions for arrays that are entirely palindrome from the start to finish (i.e. 11 22 33 44 33 22 11).

And also print out just the palindrome in new file.

research I’ve been doing online has only solutions for arrays that are entirely palindrome from the start to finish (i.e. 11 22 33 44 33 22 11).

Here’s the file to the code in case doesn’t print out right:

https://docs.google.com/document/d/1m8VzecxiRYheNTvnvW8i00t9I26cayaj3evteXXDaeQ/edit?usp=sharing

   import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Program_11_1
{
// ITP_120-002N; LEE Program_11_1
public static void main(String[] args)
{
    String file_name;
    String outputFileName = null;
    String done_string = "done";
    boolean done = false; 

    while (!done)   
    {
        file_name = JOptionPane.showInputDialog("Enter a file 
name"

   + " or done to exit: ");

        if (file_name.equals(done_string))
        {
            done = true;
            JOptionPane.showMessageDialog(null, 
"EXITING");
        }
        else        
        {
            Scanner input = get_Scanner(file_name);

            Scanner input_counter = 
get_Scanner(file_name);
            //JOptionPane.showMessageDialog(null, "File " 
+ file_name + " found ");

                outputFileName = 
JOptionPane.showInputDialog(null, "Enter the output file name: ");
            PrintWriter output_writer = 
get_Writer(outputFileName);
            //JOptionPane.showMessageDialog(null, "created 
output file: " + out_file_name);

            if ((input != null && output_writer != null && 
input_counter != null))
                {
                     //********TASK 
CODE**************

                        int num_ints = 
   size(input_counter);

                        int[] int_array = 
   return_Array(num_ints);                   

                        fill_Array(int_array, 
input);              

                        int palin_drome[] = 
   palin_Drome(int_array);                   


   print_Array(ordered_array, output_writer);
                    //*******END TASK CODE************
                 }

                input.close();
                input_counter.close();
                output_writer.close();  
            }
        }
    }


public static Scanner get_Scanner(String file_name) {
    try
    {
        File file_in = new File(file_name);
        Scanner input = new Scanner(file_in);
        return input;
    }
    catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(null, "File " + 
file_name +  " not found");
        return null;
    }
}

public static PrintWriter get_Writer(String out_file_name) {
    try 
    {
        PrintWriter output_writer = new 
PrintWriter(out_file_name);
        return output_writer;
    }
    catch (FileNotFoundException e) 
    {
        JOptionPane.showMessageDialog(null, "output file 
FAILED");
        return null;
    }
}


public static int size (Scanner input_file) {
    int counter = 0;
    while (input_file.hasNext()) {
        input_file.nextInt();
        counter++;
    }
    return counter;
}

public static int[] return_Array(int count) {
    int[] hold_ints = new int [count];
    return hold_ints;
}

public static void fill_Array(int[] int_array, Scanner input) {
    int index = 0; 
    for (int val : int_array) {
        int_array[index] = input.nextInt();
        index++;
        }
}

public static int[] palin_Drome(int[] un_ordered) {

            }
        }
    }
    return un_ordered;
}


public static void print_Array(int[] ordered, PrintWriter 
output_writer) {
    for(int i = 0; i < ordered.length; i++) {
        output_writer.print(ordered[i]);
        output_writer.print(" ");
    }
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *