[英]How to capitalize first letter in this program
我寫了大部分內容。 我只是無法弄清楚如何大寫每行的第一個字母。 問題是:
編寫一個程序,檢查文本文件中的幾個格式和標點符號問題。 程序要求輸入文件和輸出文件的名稱。 然后它將輸入文件中的所有文本復制到輸出文件,但是有以下兩個更改(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");
}
}
最簡單的方法是使用Apache commons-langs的WordUtil 。
您應該使用capitalise
方法和分隔符作為參數。
由於你的代碼已經包含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();
您可以嘗試以下正則表達式:
(\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.
在ASCII表中,大小寫只是整數,彼此相距32個位置......
嘗試這樣的事情:
String inputSent = .... //where ever it does come from...
System.out.println(inputSent.replace(inputSent.charAt(0), (char) (inputSent.charAt(0) - 32)));
或者使用像WordUtils這樣的某種APACHE庫。
我會將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);
}
}
原始textFile()的問題在於它只在它讀取的第一行應用大寫的第一個字符,空格等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.