当前位置: 首页 > 知识库问答 >
问题:

如何在这个程序中大写第一个字母

姜钧
2023-03-14

我写了大部分。我就是不知道如何大写每行的第一个字母。问题是:

编写一个程序,检查文本文件的几种格式和标点符号问题。程序要求输入文件和输出文件的名称。然后,它将所有文本从输入文件复制到输出文件,但经过以下两个更改(1)由两个或两个以上空白字符组成的任何字符串都将替换为一个空白字符;(2) 所有句子都以大写字母开头。第一个后面的所有句子都以句号、问号或感叹号开头,后面跟一个或多个空白字符。

我已经写了大部分代码。我只需要帮助每个句子的第一个字母大写。这是我的代码:

import java.io.*;
import java.util.Scanner;


public class TextFileProcessor
{
public static void textFile()
{
    Scanner keyboard = new Scanner(System.in);

    String inputSent;
    String oldText;
    String newText;


    System.out.print("Enter the name of the file that you want to test: ");
    oldText = keyboard.next();

    System.out.print("Enter the name of your output file:");
    newText = keyboard.next();
    System.out.println("\n");

    try
    {
        BufferedReader inputStream = new BufferedReader(new FileReader(oldText));
        PrintWriter outputStream = new PrintWriter(new FileOutputStream(newText));
        inputSent = inputStream.readLine();

        inputSent = inputSent.replaceAll("\\s+", " ").trim();
        inputSent = inputSent.substring(0,1).toUpperCase() + inputSent.substring(1);

        inputSent = inputSent.replace("?", "?\n").replace("!", "!\n").replace(".", ".\n");
        //Find a way to make the first letter capitalized

        while(inputSent != null)
        {
            outputStream.println(inputSent);
            System.out.println(inputSent);
            inputSent = inputStream.readLine();
        }

        inputStream.close();
        outputStream.close();
    }

    catch(FileNotFoundException e)
    {
        System.out.println("File" + oldText + " could not be located.");
    }

    catch(IOException e)
    {
        System.out.println("There was an error in file" + oldText);
    }
}
}
import java.util.Scanner;
import java.io.*;


public class TextFileProcessorDemo
{
public static void main(String[] args)
{
    String inputName;
    String result;
    String sentence;


    Scanner keyboard = new Scanner(System.in);


    System.out.print("Enter the name of your input file: ");
    inputName = keyboard.nextLine();
    File input = new File(inputName);

    PrintWriter outputStream = null;

    try
    {
        outputStream = new PrintWriter(input);
    }

    catch(FileNotFoundException e)
    {
        System.out.println("There was an error opening the file. Goodbye!" + input);
        System.exit(0);
    }

    System.out.println("Enter a line of text:");
    sentence = keyboard.nextLine();

    outputStream.println(sentence);
    outputStream.close();

    System.out.println("This line was written to:" + " " + input);
    System.out.println("\n");

}
}

共有3个答案

慕容俭
2023-03-14

您可以尝试以下正则表达式:

(\S)([^.!?]*[.!?]( |$))

>

  • 代码:

    public static void main(String[] args) {
        String inputSent = "hi! how are you? fine, thanks.";
        inputSent = inputSent.replaceAll("\\s+", " ").trim();
        Matcher m = Pattern.compile("(\\S)([^.!?]*[.!?]( |$))").matcher(inputSent);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2) + "\n");
        }
        m.appendTail(sb);
        System.out.println(sb);
    }
    

    在线观看演示。

    输出:

    Hi! 
    How are you? 
    Fine, thanks.
    

  • 锺离锦
    2023-03-14

    最简单的方法可能是使用ApacheCommonsLangs中的WordUtil。

    您应该使用以分隔符为参数的大写方法。

    邢博文
    2023-03-14

    因为你的代码已经包含inputSent=inputSent.substring(0,1). toUpperCase()inputSent.substring(1);我假设inputSent可以包含一个以上的句子,或者可能只是表示文件的一行用句子的一部分。

    因此,我建议您首先将整个文件读入一个字符串(如果不是太大),然后在该字符串上使用split(),将其拆分为单独的句子,将第一个字符大写,然后再次连接它们。

    例子:

    String[] sentences = fileContent.split("(?<=[?!.])\\s*");
    StringBuilder result = new StringBuilder();
    for( String sentence : sentences) {
      //append the first character as upper case
      result.append( Character.toUpperCase( sentence.charAt(0) ) );
      //add the rest of the sentence
      result.append( sentence.substring(1) );
      //add a newline
      result.append("\n");
    }
    
    //I'd not replace the input, but to be consistent with your code
    fileContent = result.toString();
    
     类似资料:
    • 问题内容: 用MySQL的说法,有人知道这一TSQL的等效性吗? 我试图将每个条目的首字母大写。 问题答案: 几乎相同,您只需要更改即可使用CONCAT()函数而不是+运算符: 这会变成对,对,对等,如果你想为大写首字母和小写其他的,你只需要使用LCASE函数: 请注意,UPPER和UCASE做相同的事情。

    • 问题内容: 我想在angularjs中大写字符串的第一个字符 当我用它转换整个字符串为大写。 问题答案: 使用此大写过滤器

    • 问题内容: Java中是否内置了一个函数,该函数可以大写字符串中每个单词的第一个字符,而不会影响其他单词? 例子: jon skeet -> Jon Skeet miles o’Brien-> Miles O’Brien(B仍然是大写字母,这排除了标题大小写) old mcdonald-> Old Mcdonald* (Old McDonald也可以找到,但我不认为它会那么聪明。) 快速浏览一下J

    • 请为我们第一个程序 理所当然的 Hello, World! 做好准备。 1: %include 'system.inc' 2: 3: section .data 4: hello db 'Hello, World!', 0Ah 5: hbytes equ $-hello 6: 7: section .text 8: global _start 9: _st

    • 问题内容: 有没有一种方法可以使CSS标签中的第一个字符变为 大写 。 这是我的HTML: 问题答案: 有一个属性: 如果您的链接可以包含多个单词,并且您只希望第一个单词的首字母大写,请改用其他转换(尽管并不重要)。注意,为了使工作的元件需要是块容器(其可以是,或任何各种一个或多个属性的其他组合的):

    • 问题内容: 如何在AngularJS表单元素内的输入字段中自动大写第一个字符? 我已经看到了jQuery解决方案,但相信在AngularJS中必须使用指令来完成此操作。 问题答案: 是的,您需要定义一个指令并定义自己的解析器函数: HTML: 小提琴