Java 分别分析字符串中的每个单词

Java 分别分析字符串中的每个单词,java,arrays,string,Java,Arrays,String,当我试图分别分析字符串中的每个单词时,第一个单词做得很好,但其余的都失败了。请帮助我更正此JAVA代码以获得正确的输出: public class Test { public static void main(String[] args) { int count1=0; int chars_not_x=0 ; String str = "xyz xyxzghz zyxzz"; String[] words = str.split("\\s");

当我试图分别分析字符串中的每个单词时,第一个单词做得很好,但其余的都失败了。请帮助我更正此JAVA代码以获得正确的输出:

public class Test {

 public static void main(String[] args) {


            int count1=0;
    int chars_not_x=0 ;

String str = "xyz xyxzghz zyxzz";
    String[] words = str.split("\\s");

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

            System.out.println(words[m]);

            System.out.println("Total characters in the word: "+words[m].length());
    for(int n=0; n<words[m].length(); n++){
if(words[m].charAt(n)=='x'){count1++;}}

           System.out.println("Number of x :"+count1);
           chars_not_x= words[m].length()- count1; 
           System.out.println("Chars other than x: "+chars_not_x);

           System.out.println("\n");


  }} }
所需输出:

xyz
Total characters in the word: 3
Number of x :1
Chars other than x: 2


xyxzghz
Total characters in the word: 7
Number of x :2
Chars other than x: 5


zyxzz
Total characters in the word: 5
Number of x :1
Chars other than x: 4

在循环中声明
count1
chars\u not\u x
变量,或在每次迭代中重新初始化
count1
chars\u not\u x
,并在每次迭代中声明
count1
chars\u not\u x
变量,或在循环中重新初始化
count1
chars\u not\u x
迭代。

根据需要运行代码-->

公共类测试{
公共静态void main(字符串[]args){
int count1=0;
int chars_not_x=0;
String str=“xyz xyxzghz zyxzz”;
String[]words=str.split(\\s”);
对于(int m=0;m根据需要工作代码-->

公共类测试{
公共静态void main(字符串[]args){
int count1=0;
int chars_not_x=0;
String str=“xyz xyxzghz zyxzz”;
String[]words=str.split(\\s”);

对于(int m=0;m而言,问题在于,您在错误的位置声明了递增的值,从而错误地处理了这些值。我对您的代码做了一些更改,请查看它是否有助于您:

public static void main(String[] args) {

    String str = "xyz xyxzghz zyxzz";
    String[] words = str.split(" ");

    for (String word : words) {

        int xFound = 0;
        int nonXFound = 0;
        String[] chars = word.split("(?!^)");
        for (String current : chars) {
            if ("x".equals(current)) {
                xFound++;
            } else {
                nonXFound++;
            }
        }

        System.out.println("Word [" + word + "] has [" + chars.length + "] characters, and contains [" + xFound + "] x and [" + nonXFound + "] non-x.");
    }
}
  • 字符串被拆分为单词
  • 每个单词被分成字符串,这些字符串与
    x
  • 单词的长度是拆分单词的字符串数组的大小
输出:

单词[xyz]有[3]个字符,包含[1]个x和[2]个非x字符

字[xyxzghz]有[7]个字符,包含[2]个x和[5]个非x

字[zyxzz]有[5]个字符,包含[1]x和[4]个非x


注意:用于拆分为我使用的字符的正则表达式是
“(?!^)”
,而不是
,因为后者会生成一个不需要的空白字符作为数组中的第0个元素(请尝试)。拆分为字符或单个字符串数组的方式可能不同,但这只是一个示例。

问题在于,您在错误的位置声明了递增的值,从而错误地使用了它们。我对您的代码做了一些更改,请看它是否有助于您:

public static void main(String[] args) {

    String str = "xyz xyxzghz zyxzz";
    String[] words = str.split(" ");

    for (String word : words) {

        int xFound = 0;
        int nonXFound = 0;
        String[] chars = word.split("(?!^)");
        for (String current : chars) {
            if ("x".equals(current)) {
                xFound++;
            } else {
                nonXFound++;
            }
        }

        System.out.println("Word [" + word + "] has [" + chars.length + "] characters, and contains [" + xFound + "] x and [" + nonXFound + "] non-x.");
    }
}
  • 字符串被拆分为单词
  • 每个单词被分成字符串,这些字符串与
    x
  • 单词的长度是拆分单词的字符串数组的大小
输出:

单词[xyz]有[3]个字符,包含[1]个x和[2]个非x字符

字[xyxzghz]有[7]个字符,包含[2]个x和[5]个非x

字[zyxzz]有[5]个字符,包含[1]x和[4]个非x


注意:用于拆分为我使用的字符的正则表达式是
“(?!^)”
,而不是
,因为后者会生成一个不需要的空白字符作为数组中的第0个元素(请尝试)。拆分为字符或单个字符串数组可以通过不同的方式完成,但这只是一个示例。

请确保重置每个单词的x计数:

for(int m=0; m<words.length; m++){
    System.out.println(words[m]);
    System.out.println("Total characters in the word: "+words[m].length());
    for(int n=0; n<words[m].length(); n++){
        count1 =0; //**missing** 
        if(words[m].charAt(n)=='x'){count1++;}
    }

    System.out.println("Number of x :"+count1);
    chars_not_x= words[m].length()- count1; 
    System.out.println("Chars other than x: "+chars_not_x);
    System.out.println("\n");        
}

for(int m=0;m确保为每个单词重置x的计数:

for(int m=0; m<words.length; m++){
    System.out.println(words[m]);
    System.out.println("Total characters in the word: "+words[m].length());
    for(int n=0; n<words[m].length(); n++){
        count1 =0; //**missing** 
        if(words[m].charAt(n)=='x'){count1++;}
    }

    System.out.println("Number of x :"+count1);
    chars_not_x= words[m].length()- count1; 
    System.out.println("Chars other than x: "+chars_not_x);
    System.out.println("\n");        
}

用于(int m=0;mcount1应该在循环中设置为0。现在count1仍然包含旧值,并且随着新值的增加而增加。count1应该在循环中设置为0。现在count1仍然包含旧值,并且随着新值的增加而增加。您在代码中所做的更改非常出色。它为我打开了另一个了解mor的窗口e、 由于我是新来的,请给我一些链接来学习你的“word.split”((?!^)”“代码的一部分。很高兴我能帮上忙。这叫做消极展望。你可以在这里阅读更多关于它的信息和其他很酷的正则表达式内容:你对代码的更改非常出色。这为我打开了另一个了解更多信息的窗口。由于我是新来者,请给我一些链接来学习你的”word.split((!^)”“代码的一部分。很高兴我能帮上忙。这叫做消极展望。你可以在这里阅读更多关于它和其他很酷的正则表达式的信息: