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.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 に答える