0

good afternoon, practicing exercises I am having a problem, which I already identified, but I have not managed to solve, it must be something simple that I do not see, this is what the exercise asks for:

Write a program that reads an array of ints and an integer number n. The program must sum all the array elements greater than n.

Input data format

The first line contains the size of an array. The second line contains elements of the array separated by spaces. The third line contains the number n.

Output data format

Only a single number representing the sum.

Sample Input 1:

5

5 8 11 2 10

8

Sample Output 1:

21

This is my code that I have so far:

 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int size = in.nextInt();
    int[] array = new int[size];
    int sum = 0;

    int n = in.nextInt();

    for (int i = 0; i < array.length; i++) {
        array[i] = in.nextInt();
            if (array[i] > n) {
                sum += array[i];
            }
    }
    System.out.println(sum);
}

I identify that my program prints 37 and not 21, because it is not comparing the number well, since it asks me for the value of n in the second line that I enter and it should be after it has the elements of the arrangement, I would like them to help me, try placing if, for, and did not reach the solution.

I really appreciate your time and help, thanks.

arthuro ali
  • 117
  • 1
  • 11
  • Never use nextInt(), or nextDouble() or any similar method of Scanner withou nextLine() because all of nextXXX() method ignore the last character (except nextLine() ) For more info see that https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – A L May 31 '20 at 21:37

2 Answers2

2

Order of operations is wrong. First you read array's size, then number n, and then read the array. Run this code, read the comments and compare to your code:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // read array size
    int size = in.nextInt();
    int[] array = new int[size];
    int sum = 0;

    // read elements of array
    for (int i = 0; i < array.length; i++) {
        array[i] = in.nextInt();
    }

    // and only now read the number n
    int n = in.nextInt();

    // now that we have all data, we can calculate the sum
    for (int value : array) {
        if (value > n) {
            sum += value;
        }
    }
    System.out.println(sum);
}
pafau k.
  • 1,610
  • 11
  • 20
  • 1
    This worked, thanks, apparently I had this order wrong and I had not understood the statement well, I will take it into account for future exercises, thank you very much;) – arthuro ali May 31 '20 at 21:31
  • 2
    No problem. You can also use Java 8 streams to calculate the sum: `int sum = Arrays.stream(array).filter(i -> i > n).sum()` – pafau k. May 31 '20 at 21:35
1

You should fill the array before iterating over the numbers and making the comparison. However, the question states that the numbers should be written on the same line separated by space. Here is the solution:

Scanner in = new Scanner(System.in);
int size = in.nextInt(); //read the size
int[] array = new int[size];
in.nextLine();
String nums = in.nextLine(); //read the array 
int n = in.nextInt(); //read n
int sum = 0;
String[] numbers = nums.split(" "); //split array elements
if(numbers.length == size) {
    for(int i = 0; i < numbers.length; i++) {
        int current = Integer.valueOf(numbers[i]); //convert to int
        if(current > n) //compare
            sum += current;
    }
    System.out.println(sum);
}

Sample Input/Output:

5
5 8 11 2 10
8
21
Majed Badawi
  • 16,831
  • 3
  • 12
  • 27