Soon
Soon

Reputation: 339

Analyzing each word of a string separately

When I tried to analyze each word of a string separately, the first word was done well, but I failed for the rest. Help me to correct this JAVA code for right output:

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");


  }} }

The output of the code is"

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 :3
Chars other than x: 4


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

The required output:

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

Upvotes: 1

Views: 906

Answers (4)

Shrikant Kakani
Shrikant Kakani

Reputation: 1561

Working Code as needed-->

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++){

count1 = 0;    // Add this

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");


}} }

Upvotes: 2

Dropout
Dropout

Reputation: 13866

The problem was that you declared the incremented values in a wrong place and thus worked with them incorrectly. I've changed your code a bit, take a look if it helps you:

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.");
    }
}
  • the String gets splitted into words
  • each word is split into character Strings which are compared to x
  • the length of the word is the size of the String array in which the word is splitted

Output:

Word [xyz] has [3] characters, and contains [1] x and [2] non-x.

Word [xyxzghz] has [7] characters, and contains [2] x and [5] non-x.

Word [zyxzz] has [5] characters, and contains [1] x and [4] non-x.

Note: The regex used for splitting to characters I used was "(?!^)" instead of "" because the latter one produces an unwanted blank character as a 0th element in the array (try it out). Splitting to characters or single-character String arrays can be done in different ways though, this is just an example.

Upvotes: 2

Bobz
Bobz

Reputation: 2604

Make sure you reset the count of x for each word:

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");        
}

Upvotes: 2

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Declare count1 & chars_not_x variable inside the loop or reinitialize count1 and chars_not_x with 0 on every iteration.

Upvotes: 0

Related Questions