Coralzee
Coralzee

Reputation: 67

Java invoking command line arguments from the main method to a separate method

How could I optimise this code to take the String[] games values from the main method and have a separate method: public static int points(String[] games). I am super new to Java and don't really understand how to invoke methods.

public class TotalPoints {

    public static void main(String[] args) {
        String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
        int sum = 0;
        int matches = 10;
        int x = 0;
        int y = 0;
        for (int i = 0; i < games.length; i++) {
            String[] pieces = games[i].split(":");
            x = Integer.parseInt(pieces[0]);
            y = Integer.parseInt(pieces[1]);
        }

        for (int j = 0; j < matches; j++) {
            if (x > y) {
                sum = sum + 3;
            } else if (x == y) {
                sum = sum + 1;
            }
        }

        System.out.println(sum);
    }
}

Upvotes: 1

Views: 82

Answers (3)

DLx
DLx

Reputation: 127

I suggest this to you

     public class TotalPoints {
        public static void main(String[] args) {
        String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
        int sum = points(games);
        System.out.println(sum);
    }

    private static int points(String[] games) {
        int sum = 0;
        int matches = 10;
        int x = 0;
        int y = 0;
        for (String game : games) {
            String[] pieces = game.split(":");
            x = Integer.parseInt(pieces[0]);
            y = Integer.parseInt(pieces[1]);
        }
        for (int j = 0; j < matches; j++) {
            if (x > y) {
                sum = sum + 3;
            }
            else if (x == y) {
                sum = sum + 1;
            }
        }
        return sum;
    }
}

I replace for (int i = 0; i < games.length; i++) by for (String game : games) it's a simpler way to browse a list

Upvotes: 1

Smile
Smile

Reputation: 4088

You can write something like

public class TotalPoints {

    public static void main(String[] args) {
        int sum = points(args);
        System.out.println(sum);
    }

    public static int points(String[] games) {
        int sum = 0;
        int matches = 10;
        int x = 0;
        int y = 0;
        for (int i = 0; i < games.length; i++) {
            String[] pieces = games[i].split(":");
            x = Integer.parseInt(pieces[0]);
            y = Integer.parseInt(pieces[1]);
        }
        for (int j = 0; j < matches; j++) {
            if (x > y) {
                sum = sum + 3;
            } else if (x == y) {
                sum = sum + 1;
            }
        }
        return sum;
    }
}

And when you run this class, pass the arguments from command line like java TotalPoints "1:0" "2:0" "3:0" "4:0" "2:1" "3:1" "4:1" "3:2" "4:2" "4:3"

Upvotes: 1

Jens
Jens

Reputation: 69440

very simple:

public class TotalPoints {

    public static void main(String[] args) {
       String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
       int result = points(games);
    }
    public static int points(String[] games) {
       //dowhat ever you want and return an int value

    }
}

Upvotes: 1

Related Questions