[英]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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.