Java 检查字符中的下一个字母何时在句点之后

Java 检查字符中的下一个字母何时在句点之后,java,arrays,char,Java,Arrays,Char,所以我正在写一个练习程序,我有点卡住了。我想检查char[]数组中的第一个字符是否是字母,我已经尝试了Isleter,但显然不能在char中这样做 例句:“/我的名字叫克里斯” 请注意开头的“/” 我想让第一个字母大写。是的 固定示例句子:“/我的名字是克里斯” 我的代码很可能会非常混乱和过度编码,但我还没有达到可以缩短代码的程度,我还没有那么先进。就像我说的,很可能会很混乱 代码如下: import java.io.BufferedReader; import java.io.Buffered

所以我正在写一个练习程序,我有点卡住了。我想检查char[]数组中的第一个字符是否是字母,我已经尝试了Isleter,但显然不能在char中这样做

例句:“/我的名字叫克里斯” 请注意开头的“/”

我想让第一个字母大写。是的

固定示例句子:“/我的名字是克里斯”

我的代码很可能会非常混乱和过度编码,但我还没有达到可以缩短代码的程度,我还没有那么先进。就像我说的,很可能会很混乱

代码如下:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
import java.util.StringTokenizer;


public class FileOrUserSentenceHandler 
{
Scanner keyboard = new Scanner(System.in);
String userChoice, fileName;
char period = '.';
String s;
String userSentence;




public String getUserChoice()
{

    System.out.println("Would you like the program to read from a file or a sentence from you? Type 'File' for it to read an already-created file, Type 'me' for it to read a sentence from you.");
    userChoice = keyboard.nextLine();

    return userChoice;
}

public void decideChoice() throws Exception
{
    if(userChoice.equalsIgnoreCase("file"))
    {
        pickedFile();
    }
    else if(userChoice.equalsIgnoreCase("me"))
    {
        pickedMe();
    }
    else
    {
        getUserChoice();
        decideChoice();
    }
}

public void pickedFile() throws Exception
{

    //create file object
    File outFile = new File("CreatedFile.txt");
    BufferedWriter outBWriter = new BufferedWriter(new FileWriter(outFile));

    outBWriter.write("you picked the file option. why not pick the 'me' option?");

    //close the file
    outBWriter.close();
    System.out.println("File was created, Data was written to file.");


    try
    {
        File inFile = new File("CreatedFile.txt");

        BufferedReader inBReader =  new BufferedReader(new FileReader(inFile));

        do
        {
            s = inBReader.readLine();
            if(s == null)
            {
                break;
            }

            System.out.println(s);

            System.out.println("Fixing the sentence from the file...");
            Thread.sleep(3000);

            char[] charArray = s.toCharArray();


            for(int i = 0; i < charArray.length; i++)
            {

                if(i == 0)
                {
                    charArray[i] = Character.toUpperCase(charArray[i]);
                }

                if(charArray[i] == '.')
                {
                    charArray[i + 2] = Character.toUpperCase(charArray[i + 2]);
                }
            }

            System.out.println(charArray);

        }while(s != null);
    }
    catch(Exception e)
    {
        System.out.println("Error. Closing program...");
        System.exit(0);
    }


}

public void pickedMe()
{
    System.out.println("You picked the 'me' option. Please enter a sentence below:");
    userSentence = keyboard.nextLine();

    StringTokenizer userSentenceEdited = new StringTokenizer(userSentence, " "); //creates a tokenized String of userSentence, checks the spaces
    StringBuffer sb = new StringBuffer();

    while(userSentenceEdited.hasMoreElements())
    {
        sb.append(userSentenceEdited.nextElement()).append(" ");
    }

    userSentence = sb.toString();

    //System.out.println(userSentence); // printed out to check if it was working properly

    outerloop : if((userSentence.charAt(userSentence.length() - 1) != '.'))
    {
            if(userSentence.charAt(userSentence.length() - 1) == ' ')
            {
                userSentence = userSentence.trim();
            }

            if(userSentence.charAt(userSentence.length() - 1) == '.')
            {
                //System.out.println("hello");
                break outerloop;
            }

        userSentence += '.';
    }

    //System.out.println(sb.toString().trim());


    char[] userCharArray = userSentence.toCharArray(); //converts the userSentence to a char[] array

    for(int i = 0; i < userCharArray.length; i++)
    {
        if(i == 0) //checks if it's at the beginning of the array, if so, capitalizes it.
        {
            userCharArray[i] = Character.toUpperCase(userCharArray[i]);
        }

        if(i == '.') //checks if it's at a period, if so, it checks if it's at the end of the sentence
                                    //if it is, it breaks out of the loop, which means it doesn't capitalize nothing,
                                    //if it's not the end of the String, it capitalizes 2 spaces over.
        {
            if(i == userCharArray.length)
            {
                break;
            }
            userCharArray[i + 2] = Character.toUpperCase(userCharArray[i + 2]);
        }

    }

    System.out.println(userCharArray);
}


}
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.util.Scanner;
导入java.util.StringTokenizer;
公共类FileOrUserSentenceHandler
{
扫描仪键盘=新扫描仪(System.in);
字符串userChoice,文件名;
字符周期=';
字符串s;
字符串用户语句;
公共字符串getUserChoice()
{
System.out.println(“您希望程序从您的文件中读取还是从您的句子中读取?键入'file'以读取已创建的文件,键入'me'以读取您的句子。”);
userChoice=keyboard.nextLine();
返回用户选择;
}
public void decideChoice()引发异常
{
if(userChoice.equalsIgnoreCase(“文件”))
{
pickedFile();
}
else if(userChoice.equalsIgnoreCase(“me”))
{
pickedMe();
}
其他的
{
getUserChoice();
决策选择();
}
}
public void pickedFile()引发异常
{
//创建文件对象
File outFile=新文件(“CreatedFile.txt”);
BufferedWriter outBWriter=新的BufferedWriter(新文件编写器(outFile));
write(“您选择了文件选项。为什么不选择‘我’选项?”);
//关闭文件
outBWriter.close();
System.out.println(“文件已创建,数据已写入文件”);
尝试
{
File infie=新文件(“CreatedFile.txt”);
BufferedReader Inbreard=新的BufferedReader(新文件读取器(infle));
做
{
s=inBReader.readLine();
如果(s==null)
{
打破
}
系统输出打印项次;
System.out.println(“修复文件中的句子…”);
睡眠(3000);
char[]charArray=s.toCharArray();
for(int i=0;i
再说一次,我知道它很可能非常混乱,我强调的太多了,我稍后会处理它

请客气点,这是我的第一个“大”项目。

你可以做到

boolean b = Character.isLetter(arr[0]);
其中,arr是字符数组或

boolean b = Character.isLetter(s.charAt(0));
如果您有一个字符串而不是字符数组。

您可以这样做

 if (Character.isLetter(str.charAt(0))){
     //change the first char to upper case
     str = str.substring(0,1).toUpperCase() + str.substring(1);
 }
其中
str
是您正在使用的字符串的变量名

这里有另一种方法来修复字符串,使句点后的字母始终大写,而不是遍历整个字符串并始终检查句点后下一个字母出现的位置(因为它可能在句点后出现任意数量的空格)

//split all sentences and put them into an array
String[] tokens = str.trim().split("\\.");
//initialize a result string
String resultString = " ";
//loop through every sentence capitalizing the first character.
for(int i = 0; i<tokens.length; i++){
   //first trim off any whitespace that may occur after period...
   tokens[i] = tokens[i].trim();
   //then capitalize the first letter of every sentence
   tokens[i] = tokens[i].substring(0,1).toUpperCase() + tokens[i].substring(1);
   //then add the sentence with the upperCase first character to your result string
   resultString += " " + tokens[i] + ".";
}
//once out of loop, resultString has the contents of all the sentences with a capital letter to begin the sentence. Notice I use str for my string name, you use s
//拆分所有句子并将它们放入数组中
String[]tokens=str.trim().split(\\);
//初始化结果字符串
字符串resultString=“”;
//循环使用第一个字符大写的每个句子。
对于(int i=0;i如何