Java 如何获取字符串并输出数量:空格、大写、小写等

Java 如何获取字符串并输出数量:空格、大写、小写等,java,arrays,string,arraylist,Java,Arrays,String,Arraylist,因此,我希望能够获取字符串并输出以下内容:大写、小写、数字、句点、逗号、空格和其他符号。我已经完成了大部分代码,但是我已经在我遇到问题的地方发表了评论。我不知道该怎么做,这已经困扰了我一周的大部分时间了!非常感谢您的帮助 import java.util.*; public class JavaPractice { public static void main(String[] args) { //declarations Scanne

因此,我希望能够获取字符串并输出以下内容:大写、小写、数字、句点、逗号、空格和其他符号。我已经完成了大部分代码,但是我已经在我遇到问题的地方发表了评论。我不知道该怎么做,这已经困扰了我一周的大部分时间了!非常感谢您的帮助

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
  • 首先将
    字符串
    转换为
    字符
    数组,请参阅。您还可以使用,
    toCharArray
    将使迭代字符变得更容易
  • 创建
    char
    数组的to循环
  • 对于每个
    char
    使用、、或将其与需要查找的任何其他
    char
    进行比较。这将涉及某种声明

首先将字符串设置为字符数组

对于大写和小写,当字符等于计数器的增量时,可以从a-Z进行循环


对于句号、逗号和空格,也可以这样做。也就是说,循环字符串的长度并查找句点、逗号和空格的出现次数。有关更高级的解决方案,您可以查看正则表达式。

我将使用
String
类中的split函数,该函数基于在特定正则表达式输入上拆分字符串返回数组

例如,考虑以下
字符串

String input="hello.world";

// use different delimeters
// splitting the input based on a comma
String[] test = input.split(","); 

// test will now have the value test[0] = hello , test[1]= world

int length = test.length - 1 // test.length = 2, you need to subtract 2 because the length of the array will always be 1 more than the number of occurences, so if the number of occurances is n the length of the array is n+1.

您也可以基于其他字符进行类似的拆分。

您可能需要提及您遇到的问题。更具体地说,您可能需要尝试编写代码,而不是在此处显示空白区域……除非输入看起来像“a、b、c、d、e,…”,否则不知道这有什么帮助。(顺便说一句,您的分隔符不匹配。因此,
test
将只包含一个字符串
“hello.world”
)即使如此,在某些情况下,您仍然需要使用字符来确定类型。这只是asker理解字符串函数的一个简单解决方案。字符串根据分隔符参数进行拆分。如果您维护一个输入字符串并对一个复制字符串执行操作,那么您可以为所有人执行该操作。我同意这不是内存效率最高的解决方案内存效率不是问题所在。问题是:好的,您已经拆分了字符串。恭喜。现在怎么办?拆分并没有让你离目标更近——你只需要计算一堆子字符串。任何对这些子字符串有效的方法,都应该对未拆分的字符串有效。如果你不想计算比一个字符(例如单词)更大的数字,那么拆分实际上只是让事情变得复杂。