4

Explanation

I was given a task in my Grade 12 Computer Science class that just totally has me stumped. So far I have tried using a bunch of if statements (While i know that is not the best way of doing I though for sure it was going to work) but that didn't work. I then tried to use nested loops but that didn't work either here is the code that I created so far for this method.

Code (Note: Max=1000)

private static void totals2 (Scanner user_input,int max){
        int[] num = new int[max];
        int[] total=new int[10];
        System.out.println("Welcome to totals\nThis method gathers input and displays the sum depending on which category they fall in");
        System.out.println("Enter numbers from 0-99 only (Up to 1000 inputs)\nEnter any number outside of 0-99 to stop collecting\n");
        for (int i = 0; i < num.length; i++) {
            System.out.println("Please enter a number to be stored in index: " + i);
            num[i] = user_input.nextInt();
            if (num[i] < 0 || num[i] > 99) {
                break;
            }
        }
        for(int i = 0; i < 100; i +=10){
            int j;
            int k=(i/10)-1;
            for(j=0 ;j<1000;j++){
                if (num[j] <= i){
                    total[k]+= num[j];
                }
            }
           System.out.println(total[k]);
        }
    }

Question

Create a procedure that will output the total of all numbers (in an array) entered less than 10, the total of all numbers entered less than 20, the total of all numbers entered less than 30, ... and the total of all numbers entered less than 100 (so 88 will be included in both totals for numbers less than 90 and less than 100).

Community
  • 1
  • 1
  • What is total of numbers? Sum? – Steven Sep 21 '17 at 00:13
  • 1
    We generally like a bit more detail about the problem than "...that didn't work...". Is it giving incorrect totals? Does it throw exceptions? – Kevin Anderson Sep 21 '17 at 00:26
  • Total of numbers in the sum "That didn't work" It was outputting wrong information and would not add correctly basically it would only output 1 part. – MLJBrackett Sep 21 '17 at 00:45
  • I ran your code and it's throwing `ArrayIndexOutOfBoundsException`. Is that what you mean by "outputting wrong information"? – Kevin Anderson Sep 21 '17 at 00:53
  • You need to initialize `i = 10` and change the condition to <=. And since your num length is dynamic. change second loop's condition to `j – simple guy Sep 21 '17 at 00:56
  • Thanks so much guys really helpful! – MLJBrackett Sep 21 '17 at 01:45
  • 5
    You did a better job with your first question than most people that actually have jobs writing code. Bravo! –  Sep 21 '17 at 02:27
  • I agree with @Jarrod, good work. My only tip would be to refrain from asking for formatting advice in questions themselves - that stuff naturally gets trimmed here. You can always add meta-commentary and requests to the comments if you wish. – halfer Sep 21 '17 at 11:24

3 Answers3

1

As it is your assignment, I will not post any code here. Just some thoughts.

  1. Do you need to save the number out of 0 to 99? If not, check your first loop.

  2. for your second for loop, if i=0, what value will be k? if i=10 what value will be k?

I did not run your algo, that's all issues I found while reading it. Learn how to use a debugging tool will be helpful for your further study. (Recommend Intellj for Java development)

White
  • 527
  • 2
  • 9
1

Great first question:

This already has answers for your problems, normally your question would probably get closed as a duplicate of these, but I think it should be left open so you can get some details about your code since you did such a good job with your first question.

Since you are pretty close to getting this correct I am just going to say that everything you need to get your code working is in the two questions linked below.

How to use Scanner properly:

How to use java.util.Scanner to correctly read user input from System.in and act on it?

How to iterate over an Array:

How to avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException?

Effectively Debugging:

What is a debugger and how can it help me diagnose problems?

What is a stack trace, and how can I use it to debug my application errors?

Community
  • 1
  • 1
0

First off, you can not iterate through an Array with integer index values greater than the number of indexes (- 1 because arrays are zero (0) based) contained within the Array otherwise you will simply end up with an ArrayIndexOutOfBoundsException. If there are only 20 elements contained within the Array then you can only iterate from 0 to 19....not 0 to 999. With this in mind, change 1000 to num.length and you should be fine with the exception of....

For your for loop used to increment by 10 for the "find values less than" value, initialize the variable i to 10 instead of 0. Think about it...do the math...what do you think will happen when you try to divide 0 into 10 then subtract 1 [(0 / 10) - 1]? You end up with an invalid index value for the variable k and therefore, another ArrayIndexOutOfBoundsException.

Also, if you read the question carefully you can see that what is required are the sums of values supplied that are less than 10, less than 20, less than 30....less than 100. Not Less Than or Equal To (... <= ...). Correct your if statement condition within your second for loop.

Food for thought:

You might want to ask the User to supply the max array index value as well and eliminate that as a parameter for the totals2() method. Place the explanation for it into the "Welcome" message.

DevilsHnd
  • 6,334
  • 2
  • 14
  • 19