0

好的,所以我真的不知道如何正确地表达标题,所以这应该可以说明情况。

我正在用Java制作一个回文程序。无论您以哪种方式看待它,它都可以正常工作。它使用 读取文件Scanner,搜索整个文件并输出文本文件中的该行是否为回文。如果您有任何特殊字符或大写字母,它会删除它们并将所有内容转换为小写。

我的问题是,在每行完成检查后,我想在结果旁边显示一些额外的信息。

每行应显示该行中有多少个单词,多少个字符以及是否为回文。

无论如何这里是代码,希望有人可以帮助我解决这个问题。谢谢。

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

public class Palindrome {

    public static void main(String[] args) {
        //Global Variables
        Scanner cScan = null;
        Scanner wScan = null;
        Scanner pScan = null;
        int charCount = 0, numLines = 0, numChars = 0, wordCount = 0;

        //Take in User Input
        Scanner iScan = new Scanner(System.in);     //Start input Scanner
        String fileName = null;

        System.out.print("Please Enter a File Name: ");
        fileName = iScan.nextLine();

        iScan.close();      //Close input Scanner

        //Read File Specified by User
        File palin = new File(fileName);

        try {   

            //Checks for Number of Characters
            cScan = new Scanner(palin);

            while(cScan.hasNextLine()) {

                String line = cScan.nextLine();

                numChars += line.length();
                numLines++;
            }

            //Checks for Number of Words
            wScan = new Scanner(palin);

            while (wScan.hasNext()) {

                wScan.next();
                wordCount++;
            }

            //Format Lines
            pScan = new Scanner(palin);

            while (pScan.hasNext()) {

                String line = pScan.nextLine();
                String reString = line.replaceAll("[^\\p{L}\\p{Nd}]", "");
                String lString = reString.toLowerCase();
                boolean pali = false;
                String tP = "Yes", fP = "No";

                int n = lString.length();

                for (int i = 0; i < (n / 2) + 1; ++i) {
                    if (lString.charAt(i) != lString.charAt(n - i - 1)) {
                        pali = false;
                        break;
                    }
                    else if (lString.charAt(i) == lString.charAt(n - i - 1)) {
                        pali = true;
                        break;
                    }
                }

                if (pali == true)
                    System.out.println(line + "    w: " + wordCount + ", " + " c: " + charCount + ", " + tP);
                else
                    System.out.println(line + "    w: " + wordCount + ", " + " c: " + charCount + ", " + fP);
            }

        }
        catch(Exception e) {
            System.out.println("File Could Not be Found");
        }

        //charCount = (numLines + numChars) - 1;    //Minus 1 to Compensate for EOL at EOF 
        //System.out.println(charCount);
        //System.out.println(wordCount);
        //System.out.println(spRemover);
    }       
}
4

2 回答 2

0

我稍微清理了你的代码。

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

public class Palindrome {

    int charCount = 0;
    int totalWordCount = 0;

    public static String isPalindrome(String str) {
        if(str.equals(new StringBuffer().append(str).reverse().toString())) {
            return "a";
        }
        else {
            return "not a";
        }
    }

    public static int getNumberOfWords(String str) {
        return str.isEmpty() ? 0 : str.split("\\s+").length;
    }

    public void process(File file) {
        try {
            Scanner sc = new Scanner(file);
            int i = 0;
            while(sc.hasNextLine()) {
                i++;
                String line = sc.nextLine();
                int wordCount = getNumberOfWords(line); 
                System.out.println("Line " + i + "is " + isPalindrome(line) + " palindrome. It has " + wordCount + " words and " + line.length() + " characters.");
                charCount  = charCount + line.length();
                totalWordCount = totalWordCount + wordCount; 
            }
            sc.close();
            System.out.println("There are " + i + " lines in the file with a total of " + totalWordCount + " words and " + charCount + " characters.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
         Scanner iScan = new Scanner(System.in);
         String fileName = null;

         System.out.print("Please Enter a File Name: ");
         fileName = iScan.nextLine();

         iScan.close();

         File file = new File(fileName);
         Palindrome pal = new Palindrome();
         pal.process(file);
    }
}
于 2013-09-12T22:18:45.417 回答
0
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class IteratingFileWithInformation {

    int charCount = 0 ;
    int totalWordCount = 0;

    private static String checkPalindrome(String line) {

        return line.equals(new StringBuffer().append(line).reverse().toString()) ? "a" : "not a" ;

    }

    private static int getNumberOfWords(String words) {
        return words.isEmpty() ? 0 : words.split("\\s+").length;
    }

    private void checkFileAndProcess(BufferedReader file) {

        Scanner input = new Scanner(file);
        int i = 0;
        while(input.hasNextLine()) {
            i++;
            String line = input.nextLine();
            int wordCount = getNumberOfWords(line);
            System.out.println("Line: " + i + " is " + checkPalindrome(line) + " Palindrome. It has " + wordCount + " words and " + line.length() + 
                                 " characters. ");
            charCount += line.length();
            totalWordCount += wordCount; 
        }
        input.close();
        System.out.println("There are " + i + " lines in the file with a total of " + totalWordCount + " words and " + charCount + " characters.");
    }

    public static void main(String[] args) throws FileNotFoundException {

        Scanner givefileName = new Scanner(System.in);
        String fileName = null;
        System.out.println("Enter the file name :");
        fileName = givefileName.nextLine();
        givefileName.close();

        FileReader file = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(file);
        IteratingFileWithInformation fileWithInformation = new IteratingFileWithInformation();
        fileWithInformation.checkFileAndProcess(bufferedReader);
    } 
}
于 2017-01-29T18:37:19.733 回答