Java我想打印字符串中的字符数

Java我想打印字符串中的字符数,java,Java,我正在打印从用户输入中提取的字符数。假设用户输入这里是一个随机测试,总共17个字符。这里是我到目前为止只打印在单独的行字 import java.text.*; import java.io.*; public class test { public static void main (String [] args) throws IOException { BufferedReader input = new BufferedReader(new InputStr

我正在打印从用户输入中提取的字符数。假设用户输入
这里是一个随机测试
,总共17个字符。这里是我到目前为止只打印在单独的行字

import java.text.*;
import java.io.*;


public class test {
    public static void main (String [] args) throws IOException {

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String inputValue;

        inputValue = input.readLine();
        String[] words = inputValue.split("\\s+");

        for (int i = 0; i < words.length; i++) {
           System.out.println(words[i]);
        }

    }
}
import java.text.*;
导入java.io.*;
公开课考试{
公共静态void main(字符串[]args)引发IOException{
BufferedReader输入=新的BufferedReader(新的InputStreamReader(System.in));
字符串输入值;
inputValue=input.readLine();
String[]words=inputValue.split(\\s+);
for(int i=0;i
要获得总计数,必须将每个单词的计数分配给一个变量。然后在for循环之后打印它

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

        count =  count + words[i].length();
     }

     System.out.println(count );
int count=0;
for(int i=0;i
将for…循环更改为:

int total = 0;
for (int i = 0; i < words.length; i++) {
       total += words[i].length();
}
System.out.println(total);
int-total=0;
for(int i=0;i
本质上,我们在单词数组中循环,获取每个单词的长度,然后将该数量的字符添加到总计数器中。

Fewmodification done

打印文字和用户输入的长度

 public static void main(String[] args) throws IOException {

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String inputValue;

        inputValue = input.readLine();
        String[] words = inputValue.split("\\s+");

        System.out.println("Length of user input = " + inputValue.length());

        for (int i = 0 ; i < words.length ; i++) {
            System.out.println(words[i]);
            System.out.println("Length of word = " + words[i].length());
        }

    }
str.replaceAll(“\\s+”,”)
删除
str
中的所有空白,并将结果字符串分配给
str

str.length()
返回字符串中的字符数
str

所以,当您从用户处获得输入时,请执行以下操作

inputValue=inputValue.replaceAll("\\s+","");
System.out.println(inputValue.length());

如果只关心空格,则可以执行以下操作:

inputValue = input.readLine();
int len = inputValue.replaceAll(" ", "").length(); //replacing won't effect the original string and will also replace spaces.
System.out.println(len);
System.out.println(inputValue);
因此,o/p适用于您提供的样品:

17
here is a random test

我认为我们可以避免在单词长度上的迭代,若我们假设字符串只由空格分隔。以下是一个例子:

 public static void main(String args[]) {
    String test = "here is a random test";
    String[] array = test.split("\\s+");
    int size = array.length > 0 ? (test.length() - array.length + 1) : test.length();
    System.out.println("Size:" + size);
}

你想要什么?什么是问题提示:words[i].length()返回一个单词中的字符数。好的,我想这可能足够了。。。。谢谢laune在你写答案之前阅读我的评论。不编译(字符串不响应
长度
)@laune,为什么有人会/应该在写答案之前看你的评论(在已经写了之后说这句话很愚蠢,但我离题了?如果你觉得你已经回答了这个问题;回答它-在适当的地方。所有人都向@ChiefTwoPencils致敬,但也许你没有看到答案,因为它发布3分钟后?@laune,请不要向我欢呼。读了这篇评论和你提到的那篇评论后,我问了一个诚实的问题。我不明白你说的时间线。标记化的单词永远不会包含空格。为什么不干脆
System.out.println(inputValue.replaceAll(“\\s+”,“).length())
 public static void main(String args[]) {
    String test = "here is a random test";
    String[] array = test.split("\\s+");
    int size = array.length > 0 ? (test.length() - array.length + 1) : test.length();
    System.out.println("Size:" + size);
}