通过Java中的文本文件进行迭代,以便每行都包含有关该行的信息

时间:2013-09-12 21:23:56

标签: java java.util.scanner palindrome

好的,所以我真的不知道怎么说这个标题是正确的,所以这应该对情况有所了解。

我正在用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);
    }       
}

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分: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);
    } 
}