0
public class count {
private static int countPositive(int[] elems) {
    int positive = 0;
    for (int i=0;i<elems.length;i++) {
        if (elems[i] > 0){
                positive++;
        }
    }
    return positive;
}
public static void main(String[] args) {

    for(int i=0;i<args.length;i++)
        //int x =Integer.parseInt(args[i]);
    //System.out.println(countPositive(new int[]));
 }
}

here i want to convert every str "numbers" in int number,but i have no idea how to write it. Do i have to add another array to save these int numbers,and then call the countPositive? please offer some help

my purpose is to write a command line argument, and it give me a number of positive numbers,for example

> java count 1 2  3 4 5 -1 -2 -3
5  

> java count 0 -1 -2 -3 -3 -4  
0
2
  • so what is the error or problem? Commented Jan 21, 2017 at 14:58
  • "Do i have to add another array to save these int numbers,and then call the countPositive?" Yes. Commented Jan 21, 2017 at 14:59

3 Answers 3

1

Your current code shows effort, is on the right track, and actually contains all the pieces you need to actually get a working solution. I refactored your countPositive() method to directly accept the string array of command line arguments. Note that there is no reason to create an integer array; you can simply parse each command line argument and analyze it on the fly.

public class count {
    private static int countPositive(String[] elems) {
       int positive = 0;
       for (int i=0; i < elems.length; i++) {
           int element = Integer.parseInt(elems[i]);
           if (element > 0) {
               positive++;
           }
       }

       return positive;
    }

    public static void main(String[] args) {
        int count = countPositive(args);
        System.out.println(count));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is better than adding a new int array`. +1.
0

You have two choices - either add a second array, or count the positive numbers on the fly.

First solution - first transform the Strings to ints, and then count the positives ones:

public class count {
    private static int countPositive(int[] elems) {
        int positive = 0;
        for (int i = 0; i < elems.length; i++) {
            if (elems[i] > 0) {
                positive++;
            }
        }
        return positive;
    }

    public static void main(String[] args) {

        int[] intArr = new int[args.length];
        for (int i = 0; i < args.length; i++) {
            intArr[i] = Integer.parseInt(args[i]);
        }
        System.out.println(countPositive(intArr));
    }
}

Second solution - transform the Strings to ints one at a time, and count the positive ones:

public class count {
    public static void main(String[] args) {
        int positive = 0;
        for (int i = 0; i < args.length; i++) {
            if (Integer.parseInt(args[i]) > 0) {
                positive++;
            }
        }
        System.out.println(positive);
    }
}

A bonus, third solution using Streams:

public class count {
    public static void main(String[] args) {
        System.out.println(Arrays.stream(args).mapToInt(i -> Integer.parseInt(i)).filter(i -> i>0).count());
    }
}

Comments

0

You can start by copying the String[] args to an int[] (and then use that to call countPositive). Like,

public static void main(String[] args) {
    int[] values = new int[args.length];
    for (int i = 0; i < args.length; i++) {
        values[i] = Integer.parseInt(args[i]);
    }
    System.out.println(countPositive(values));
}

or, in Java 8+, do it all with Stream(s) like

System.out.println(Stream.of(args).mapToInt(Integer::parseInt)
        .filter(val -> val > 0).count());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.