有人能告诉我一个代码,用于检查字符串中特定数量的空格。如何检查空格的字符串
示例:用户输入的字符串需要正好具有3个空格,例如“0 4 0 1”我需要一个代码来检查每个数字之间是否有空格。
这里是我的尝试吧:
String guessedSpacesCheck = "[0-9]+";
boolean b2 = guessedSpaces.matches(guessedSpacesCheck);
int totalSpace=guessedSpaces.split(" ").length-1;
boolean isSpaceValid = totalSpace==whiteSpace;
while (b2==false && isSpaceValid==false)
{
System.out.println("Your input is not valid. Try again.");
System.out.print("Please enter the letter you want to guess: ");
letterGuess = keyboard.next().charAt(0);
System.out.print("Please enter the spaces you want to check (separated by spaces): ");
guessedSpaces = guessSpace.nextLine();
guessedSpaces.matches(guessedSpacesCheck);
if (guessedSpaces.matches(guessedSpacesCheck)==true && isSpaceValid==true)
{
b2=true;
isSpaceValid=true;
}
}
不错的要求。请展示一些努力 – Sanjeev
我很抱歉,我试图尝试解决这个问题。 – MikeJ