简体   繁体   中英

How to count character occurrences in a string from command line arguments - Java

I need to count occurrences of a character in a string that is passed in the command line. For example, if we pass a sentence "the bread was wet", and we are looking for occurrences of the character "e", the program should output the occurrences of "e" per word: 1 1 0 1 .

I am new to Java, and what I came up with doesn't work:

    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i < args.length; i++) {
            // System.out.println(args[i]+" ");
            if (args[i].charAt(i) == 'e') {
                count++;

            }
            System.out.println(count);
        }
    }

Obviously, args[i].charAt(i) is wrong here because it is looking at the 1st letter of the 1st word, as it seems. However, args.charAt(i) doesn't even work. I am not sure how to fix this and get the correct output.

To solve it you need to have two loops.

  • One for traverse the args array
  • Second to traverse characters in argument

What you are doing now you are take the i with represent position of argument args[i] (good), and with the same i you try to fetch the character charAt(i) (wrong).

In other words for first argument you check first character only, for second the second, ..., for n argument you check n character of that argument.

To avoid in future such problem try to use more functions, a function should focus on single problem

public static void countCharOccurence(String input, char search) {
    int cound = 0;
    for(int i=0; i< input.length; i++){
      if(search == input.getAt(i) {
        count++;
      }
    }
}

Try to add this in your main method. Good luck.

Something like so (the first loop goes through arguments array, the second loop goes through particular argument):

int count = 0;
for(int i=0; i<args.length; i++ ){
    count = 0;
    for (int j = 0; j<args[i].length();j++){
        if (args[i].charAt(j) == 'e'){
        count++;

        }
    }
    System.out.println(count+" ");
}

You need a second loop which loops through the args[i]

public static void main (String[] args) {
        int count = 0;
        for(int i=0; i<args.length; i++ )
    {
        int count = 0;
        //System.out.println(args[i]+" ");
        String test = args[i];
        for(int j=0;j<test.length();j++)
        {
            if (args[i].charAt(j) == 'e'){
                count++;
            }


        }
        System.out.println(count);

    }

            }

you need to do like this ...

       public static void main(String ar[]) 
        for(String a:ar)
         {
        char b[] = a.toCharArray();

           for(char c:b)
           {
               if(c='e')
                {
                        // do some code
                }
            }  

            }  

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM