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

时间:2016-04-14 16:13:22

标签: java string

我写了大部分内容。我只是无法弄清楚如何大写每行的第一个字母。问题是:

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

}
}

5 个答案:

答案 0 :(得分:1)

最简单的方法是使用Apache commons-langs中的WordUtil

您应该使用capitalise方法并将分隔符作为参数。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

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

regex

  • 代码:

    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);
    }
    

    在线查看demo

  • 输出:

    Hi! 
    How are you? 
    Fine, thanks.
    

答案 3 :(得分:0)

在ASCII表格中,大写字母只是彼此相距32个位置的整数......

尝试这样的事情:

String inputSent = .... //where ever it does come from...
System.out.println(inputSent.replace(inputSent.charAt(0), (char) (inputSent.charAt(0) - 32)));

或使用某种类型的APACHE库,例如WordUtils

答案 4 :(得分:0)

我会将textFile()更改为以下内容:

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));
        while ((inputSent = inputStream.readLine()) != null) {
            char[] chars = inputSent.toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            inputSent = new String(chars);

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

            inputSent = inputSent.replace("?", "?\n").replace("!", "!\n").replace(".", ".\n");
            System.out.println("-> " + inputSent);
            outputStream.println(inputSent);

        }
        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);
    }
}
  1. 这将逐行读取文本文件。
  2. 在第一个字符上面
  3. 在while循环中执行此操作
  4. 原始textFile()的问题在于它只在它读取的第一行应用大写的第一个字符,空格等。