0

I need to create a program that takes integer command-line arguments. The first argument would be the count of the remaining arguments that are between 1 and 100 (inclusive of 1 and 100). It then prints out the count of numbers entered that are less than or equal to 50 and the count of numbers that are greater than 50 from those remaining arguments.

The code only works if I type 0 or 1 into the command prompt. Any other value returns an ArrayIndexOutOfBoundsException. I thought if I set my array equal to "count", it would return however many numbers I typed out when printing my code, but it's still not working.

Can anyone make suggestions to my existing code block? I'm a novice programmer, so I'm at a loss at where to start. I've tried changing the array value around but keep getting the same error.

public class Distribution100 {
  public static void main(String[] args) {

    int count = Integer.parseInt(args[0]);

    int[] array = new int[count];

    int low = 0;
    int high = 0;
    for (int i = 0; i < count; i++) {

      array[i] = Integer.parseInt(args[i]);

      if (array[i] >= 1 && array[i] <= 50) {
        low++;
      } else if (array[i] > 50 && array[i] <= 100) {
        high++;
      }
    }

    System.out.println(low + " numbers are less than or equal to 50.");

    System.out.println(high + " numbers are higher than 50.");

  }

}
blackbrandt
  • 2,010
  • 1
  • 15
  • 32

1 Answers1

0

This code worked for me. Notice that I've changed one line from your code - you had array[i] = Integer.parseInt(args[i]);, and I changed it to array[i] Integer.parseInt(args[i + 1]);, as i starts at 0 and args[0] is your count, not one of the numbers to check.

public static void main(String[] args) {

        int count = Integer.parseInt(args[0]);

        int[] array = new int[count];

        int low = 0;
        int high = 0;
        for (int i = 0; i < count; i++) {

            array[i] = Integer.parseInt(args[i + 1]);

            if (array[i] >= 1 && array[i] <= 50) {
                low++;
            } else if (array[i] > 50 && array[i] <= 100) {
                high++;
            }
        }

        System.out.println(low + " numbers are less than or equal to 50.");

        System.out.println(high + " numbers are higher than 50.");

    }
alonkh2
  • 533
  • 2
  • 11
  • Thanks, alonkh2! However, somehow the code still isn't working for me. I keep getting the exception error :( – gizmo.java Sep 21 '21 at 20:13
  • I see now! The first number I'm entering is how many numbers I'm wanting to type that are between 1 and 100. For some reason, that just wasn't clicking with me. Thanks so much for the help alonkh2 and user16320675! – gizmo.java Sep 21 '21 at 20:20
  • @gizmo.java it's no problem at all. You are always more than welcome to talk to me if you need help. I'm not super advanced in java, but I know a thing or two and would love to help. – alonkh2 Sep 21 '21 at 20:22
  • Hey alonkh2! I thought I would take advantage of your offer of help to ask about another program I'm struggling with ;) Would you mind hopping over to my profile and clicking on my question titled "Names and Average Numbers"? Thanks so much! – gizmo.java Sep 25 '21 at 22:58
  • @gizmo.java hey, it's closed. We can hop on a private chat and I can try to help if you'd like :) – alonkh2 Sep 26 '21 at 12:24
  • https://chat.stackoverflow.com/rooms/237522/java-with-alonkh2 – alonkh2 Sep 26 '21 at 12:29
  • Thanks, alonkh2! Looks like I can't do a private chat since I just joined Stack a few days ago. I've been messing around with my code some more and may have figured out what I need. Still working on a few things. If I run into more issues I may post a new question on my page. Thanks again for the offer! – gizmo.java Sep 26 '21 at 15:06
  • 1
    @gizmo.java it's no problem, if you want my contact details or something i'd be happy to help with anything in the future – alonkh2 Sep 26 '21 at 17:06
  • 1
    I've gotten a little further with my code. I'm about to post a "Using Strings and Double Arrays" on my page. Would you mind taking a look at it? – gizmo.java Sep 26 '21 at 17:44