QuestionsAndAnswers
QuestionsAndAnswers

Reputation: 81

Ignoring of negative values from Java Arraylist for calculations

The idea of my code is that it asks user the income of each month, until the user imputs negative value, which should not be added to the total income and which should be shown in output, but ignored in calculations. Then the code calculates the total income (ignoring the last negative value), the average income (ignoring the negative value) and the biggest/maximum value of all values. I don't have problems getting right that maximum value. But how could I ignore the negative income in calculations, and maby even not to add it at all to the array?

The problem is that the calculation adds also the negative value/income to the total sum and average income calculations. And it does not ignore the month of the negative income.

Here is my code so far:

package income;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class Income {

    public static void main(String[] args) {
        int sum = 0;
        int months = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Write the income of each month.");

         ArrayList<Integer> array = new ArrayList<Integer>();
         System.out.println("Write the income of month 1: ");
            int income = input.nextInt();
            sum += income;
            months++;
            array.add(income);

        while(true){
            months++;
            System.out.println("Write " + months + ". month income: ");
            if(income >= 0){
            income = input.nextInt();
            sum += income;

            array.add(income);

            }
            else if (income < 0){
                break;
            }
        }

        /*This did not work
          for(int i= 0; i < array.size(); i++){
              if(income < 0){
                  array.remove(i);
              }
          }*/
        months--;

        System.out.println("The total income is " + sum);
        System.out.println("The average income is " + sum/months);

        //This one below works fine
        System.out.println("The biggest income is " + Collections.max(array));
        } 
    }

Upvotes: 1

Views: 1508

Answers (3)

S B
S B

Reputation: 394

You could use the Integer.signum(int) function to know whether the value is negative (returned value = -1) or zero (returned value = 0) or positive (returned value = 1). So, basically ignore if Integer.signum(income) == -1

Upvotes: -1

user12281740
user12281740

Reputation:

I suppose this is what you are looking for.

    var list = new ArrayList<Integer>();
    var in = new Scanner(System.in);
    var i = 0;

    System.out.println("Write Income of Each Month.");

    while (true)
    {
        System.out.print("Write " + ++i + ".month income : " );
        int num = in.nextInt();

        if (num < 0)
        {
            int sum = list.stream().mapToInt(Integer::intValue).sum();
            double avg = (double) sum / --i;
            int max = Collections.max(list);

            System.out.printf("Sum : %d\nAverage : %f\nBiggest Number : %d\nNegative No. %d", sum ,avg, max, num);
            break;
        }
        else
        {
            list.add(num);
        }
    }

Upvotes: 0

Sweeper
Sweeper

Reputation: 271175

Although you are indeed adding the last negative number into account in the calculations, this is not the ultimate reason why your code is not working. You are actually checking whether the previous input you read is greater than 0 here:

while(true){
    months++;
    System.out.println("Write " + months + ". month income: ");
    if(income >= 0){ <------
    income = input.nextInt();

So the loop will only stop if the previous input is less than 0. In other words, when you enter e.g. -1 in, the input is not checked until the next iteration of the loop, at which point -1 has already been added to the array. Therefore, you should instead check income >= 0 immediately after nextInt:

System.out.println("Write the income of each month.");
ArrayList<Integer> array = new ArrayList<>();
while(true){
    months++;
    System.out.println("Write the income for month" + months + ":");
    int income = input.nextInt();
    if(income >= 0){
        sum += income;

        array.add(income);

    }
    else {
        break;
    }
}

Note that I've also removed the bit between Write the income of each month. and the while loop, as it is redundant.

Upvotes: 2

Related Questions