所以我正在编写一个练习程序,但我有点卡住了。我想检查char[]数组中的第一个字符是否是字母,我尝试过isLetter,但显然在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);
}
}
再说一遍,我知道它很可能很乱,我再怎么强调也不为过,我稍后会处理它。
请友善一点,这是我的第一个"大"项目。
您可以这样做。。。
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
您可以这样做。
boolean b = Character.isLetter(arr[0]);
其中arr是字符数组或
boolean b = Character.isLetter(s.charAt(0));
如果您有一个字符串s而不是一个char数组。