Search code examples
javapalindrome

Simple Error(While Loop) Help In my Java Palindrome Program


I am building a palindrome program in Java. The assignment is to read the words from a file, determine if the words in the file are a palindrome or not, and send the result to another file. I have everything working but the only problem in my program is that it will only read the first word. I need it to read all of the lines in the file. My code is:

import java.util.Scanner;
import java.io.*;

public class str1 {

    public static void main(String args[]) {
      try {

      String reverse = "";
      System.out.print("Enter the name of the input file: ");
      Scanner keyboard = new Scanner(System.in);
      String a = keyboard.nextLine();
      int length = a.length();
      File inFile = new File(a);
      Scanner fin = new Scanner(inFile);
      System.out.print("Enter name of the output file: ");
      String outFileName= keyboard.nextLine();
      File outFile = new File(outFileName);
      PrintWriter fout = new PrintWriter(outFile);
      while ( fin.hasNext(a) ) {
      for ( int i = length - 1; i >= 0; i-- )
     reverse = reverse + a.charAt(i);

  if (a.equals(reverse))
     fout.println("Entered string is a palindrome.");
  else
     fout.println("Entered string is not a palindrome.");
      }
      fin.close();
      fout.close();
      System.out.print("Done. See '" + outFileName + "'.");
      } catch (Exception e) {
          e.printStackTrace();
      }
    }
}

I tried to change the while in line 23 to "while ( fin.hasNextLine(a) ) {" but I have no success. I believe this is why it wont read past the first line. Any help is greatly appreciated.


Solution

  • This should work. You need to add nextLine method after checking while condition for an input file, to actually process it, and also check the length of the word after reading it:

    import java.io.File; import java.io.PrintWriter; import java.util.Scanner;

    public class Str1 {
    
        public static void main(String args[]) {
    
            try {
    
                String reverse = "";
                System.out.print("Enter the name of the input file: ");
                Scanner keyboard = new Scanner(System.in);
                String a = keyboard.nextLine();
                File inFile = new File(a);
                Scanner fin = new Scanner(inFile);
                System.out.print("Enter name of the output file: ");
                String outFileName = keyboard.nextLine();
                File outFile = new File(outFileName);
                PrintWriter fout = new PrintWriter(outFile);
                while (fin.hasNext()) {
                    String temp = fin.nextLine();
                    int length = temp.length();
                    for (int i = length - 1; i >= 0; i--)
                        reverse = reverse + a.charAt(i);
    
                    if (a.equals(reverse))
                        fout.println("Entered string is a palindrome.");
                    else
                        fout.println("Entered string is not a palindrome.");
                }
                keyboard.close();
                fin.close();
                fout.close();
                System.out.print("Done. See '" + outFileName + "'.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }