-3 java arrays string arraylist
所以我希望能够获取一个字符串并输出以下内容:大写,小写,数字,句点,逗号,空格和其他符号.我已完成大部分代码,但我在该区域评论说我遇到了麻烦.我不能怎么做,而且它让我在一周的大部分时间里都难过!非常感谢任何帮助!
import java.util.*;
public class JavaPractice
{
public static void main(String[] args)
{
//declarations
Scanner keyboard = new Scanner(System.in);
char tryAgain = 'n';
char linechar = ' ';
String lineText;
int uppercase = 0;
int lowercase = 0;
int digits = 0;
int periods = 0;
int commas = 0;
int blanks = 0;
int others = 0;
do
{
// initialize categories
uppercase = 0;
lowercase = 0;
digits = 0;
periods = 0;
commas = 0;
blanks = 0;
others = 0;
// user input
System.out.println("Enter a line of text:");
lineText = keyboard.nextLine();
// This is where I would like to count the number of spaces, uppercase ETC where I am having the most trouble
// print output
System.out.println("Uppercase:") + (uppercase);
System.out.println("Lowercase:") + (lowercase);
System.out.println("Digits:") + (digits)
System.out.println("Periods:") + (periods);
System.out.println("Commas:") + (commas);
System.out.println("Blanks:") + (blanks);
System.out.println("Other Symbols":) + (others);
// try again
System.out.println("Would you like to try again y/n?");
tryAgain = keyboard.nextLine().charAt(0);
} while (tryAgain == 'y' || tryAgain == 'Y'); //end while loop
System.out.println("GoodBye");
} //end main()
} //end JavaPractice
Run Code Online (Sandbox Code Playgroud)
String为char数组,请参阅String#toCharArray.您也可以使用String#charAt,toCharArray将更容易迭代字符.for-loop循环char.char使用,Character#isWhiteSpace,Character#isLowerCase,Character#isUpperCase,Character#isDigit,Character#isLetter或与任何其他比较一下char,你需要寻找.这将涉及某种if-else声明