简单错误(While 循环)帮助我的 Java 回文程序

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

我正在 Java 中构建一个回文程序。作业是从文件中读取单词,判断文件中的单词是否为回文,并将结果发送到另一个文件。我的一切正常,但我的程序中唯一的问题是它只会读取第一个单词。我需要它来读取文件中的所有行。我的代码是:

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();
      }
    }
}

我试图将第 23 行的 while 更改为 "while ( fin.hasNextLine(a) ) {",但我没有成功。我相信这就是为什么它不会读过第一行的原因。非常感谢任何帮助。

这应该有效。你需要在检查输入文件的while条件后添加nextLine方法,真正处理它,并在读取后检查单词的长度:

进口java.io.File; 导入 java.io.PrintWriter; 导入 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();
        }
    }
}

@QQ_At_The_PewPew,你犯了一个错误,你在字符串 "a" 上应用反向,这是你的文件名,而不是你从文件中输入的。

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() ) {
      String s=fin.nextLine();
      for ( int i = length - 1; i >= 0; i-- )
     reverse = reverse + s.charAt(i);

  if (s.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();
      }
    }
}