gpt4 book ai didi

java - 如何将这个程序中的第一个字母大写

转载 作者:行者123 更新时间:2023-11-29 04:47:27 25 4
gpt4 key购买 nike

我已经写了大部分内容。我只是不知道如何将每行的第一个字母大写。问题是:

编写一个程序来检查文本文件中的几种格式和标点符号问题。该程序要求输入文件和输出文件的名称。然后它将所有文本从输入文件复制到输出文件,但有以下两个更改 (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");

}
}

最佳答案

由于您的代码已经包含 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();

关于java - 如何将这个程序中的第一个字母大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36628513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com