简体   繁体   English

如何读取文本文件并将其存储/打印到控制台只有小写字母且没有空格?

[英]How can I read a text file and store/print it to the console with only lowercase letters and no spaces?

I am trying to take in a text file (UTF-8) and store/print it with the following constraints (see example below): 我试图接受一个文本文件(UTF-8)并使用以下约束存储/打印它(参见下面的示例):

  1. Only letters, no special characters (az) 只有字母,没有特殊字符(az)
  2. If the letter is uppercase, convert to lower case 如果字母是大写,则转换为小写
  3. No spaces or lines between letters 字母之间没有空格或线条

I first tried using scanner to read until the end of the file. 我首先尝试使用扫描仪读取,直到文件结束。 I converted the string read to a char array but could not make it lowercase correctly (see commented out code). 我将字符串读取转换为char数组但无法正确地使其小写(请参阅注释掉的代码)。 I have tried using BufferedReader, but can't get the correct changes. 我尝试过使用BufferedReader,但无法获得正确的更改。 I tried reading in by character and using the character class to set a lowercase restriction (see code below). 我尝试按字符读取并使用字符类设置小写限制(请参阅下面的代码)。

public static void OpenPlainText(File plainTextFile)
{
    try
    {
        Scanner scanner = new Scanner(plainTextFile);
        BufferedReader br = new BufferedReader(new InputStreamReader
                 (new FileInputStream (plainTextFile), Charset.forName("UTF-8")));
        StorePlainText(scanner, br);
        scanner.close();
        br.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

public static void StorePlainText(Scanner scanner, BufferedReader br) throws IOException
{
    /*String str = "";
    while (scanner.hasNext())
    {
        str += scanner.next();
    }
        str.toCharArray();
        str.toLowerCase();
        System.out.println(str);*/
    int c;
    while ((c = br.read()) != -1)
    {
        char character = (char) c;
        if (Character.isLetter(character))
        {
            if (Character.isUpperCase(character))
            {
                Character.toLowerCase(character);
                System.out.print(character);
            }
        }
    }
}

Ex. 防爆。 Text File 文本文件

Art of Computer Programming, Volume 1, Fascicle 1, The: MMIX -- A RISC Computer for the New Millennium 计算机程序设计艺术,第1卷,分册1,:MMIX - 新千年的RISC计算机

This multivolume work on the analysis of algorithms has long been recognized as the definitive description of classical computer science. 这种关于算法分析的多卷工作一直被认为是对经典计算机科学的权威性描述。

Ex. 防爆。 Store/Print 存储/打印

artofcomputerprogrammingvolumefasciclethemmixarisccomputerforthenewmillenniumthismultivolumeworkontheanalysisofalgorithmshaslongbeenrecognizedasthedefinitivedescriptionofclassicalcomputerscience artofcomputerprogrammingvolumefasciclethemmixarisccomputerforthenewmillenniumthismultivolumeworkontheanalysisofalgorithmshaslongbeenrecognizedasthedefinitivedescriptionofclassicalcomputerscience

Character.toLowerCase returns the lower case character it does not convert the one you pass in. Character.toLowerCase返回小写字符,它不会转换您传入的字符。

while ((c = br.read()) != -1)
{
    char character = (char) c;
    if (Character.isLetter(character))
    {
        if (Character.isUpperCase(character))
        {
            character = Character.toLowerCase(character);
            System.out.print(character);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用循环将字符串转换为仅包含小写字母(无空格,撇号)的新字符串 - How to make a String into a new one with only lowercase letters (no spaces, apostrophes) using a loop 如何读取文本文件并将其打印到控制台窗口? 爪哇 - How do you read a text file and print it to the console window? Java 如何从文本文件java中读取数字和字母的组合 - How can I read a combination of numbers and letters from a text file java 如何检查两个字符串是否具有相同的字母,但只打印一次常见的字母? - How can I check if two strings have the same letters, but only print the common letters once? 仅将小写字母减小为小写字母 - Decrementing lowercase letters to lowercase letters only 我可以在Java中读取二进制文件并打印出文本文件吗? - Can I read a binary file and print out a text file in java? 如何使用空格android java读取文本文件 - how to read a text file with spaces android java 如何仅使用扫描程序读取文件并将每个句子存储在arrayList中? - How can I read a file and store each sentence in an arrayList using Scanner only? 如何读取用行而不是Java中的空格分隔的控制台参数? - How can I read the console arguments separated by line instead of spaces in java? 您如何读取文本文件并使每4个单词全部大写到控制台窗口? - How do you read a text file and make every 4 words all capital print to the console window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM