John Cena John Cena - 21 days ago 5
Java Question

Repeat asking a series of numbers until the series is valid

My goal is to ask the user a series of numbers, and it will only stop asking for more numbers once he reaches the maximum of 20 numbers or when he inputs a negative number. But I only want to use the series when the sum of the numbers is under 50 and there's no number superior than 15.

This is what I have written so far and I can't understand why it doesn't work

import java.util.Scanner;

class numbersSeries {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int[] numbers = new int[20];

boolean invalid = true;
int sum = 0, input = 0, counter = 0;

while (invalid == true) {
System.out.print("Series ");
while ((input = in.nextInt()) > 0 && counter < 19) {
numbers[counter] = input;
sum += numbers[counter];
boolean hasbignumber = false;

if(numbers[counter] > 15) {
hasbignumber = true;
}
if(sum < 50 && hasbignumber == false) {
invalid = false;
}
counter++;
}
}
}
}


EDIT!!
I forgot to mention something very important. Once the series is discarded the loop will start all over again, take this output for example

Series 1 2 4 30 -3 //this series will be discarded because the fourth value surpasses the maximum of 20
Series 4 9 8 8 3 8 6 1 15 15 -2 //this one too because the sum is over 50
Series 3 1 9 0 -2 //only this one is going to be used because it fits all conditions


So all I want is for the compiler to keep asking for new series until a valid one is introduced so I can use it posteriorly.

Answer

You should not use an embedded while here since it is not needed.

it will only stop asking for more numbers once he reaches the maximum of 20 numbers or when he inputs a negative number

while ((input = in.nextInt()) > 0 && counter < 19) condition means the loop exits if 0 or less value is submitted as input or the variable counter reaches 19.
0 is not a negative value as you wish and the maximum number of 20 cannot be reached with this condition.

The boolean variable name is misleading :

 while (invalid == true) {

give the feeling that we expect to have a valid input to exit while it is not your need.

You should use a boolean variable name which conveys your rule :

For example for this rule :

I only want to use the series when the sum of the numbers is under 50 and there's no number superior than 15.

Your could use the boolean variable mustKeepSerie. And to exit the loop you could put the boolean expression in the while condition.


At the beginning,mustKeepSerie is set to true as no violation of the rule is possible as the input has not started yet.
If at any time, the sum of the numbers is superior to 50 and there's at least a number superior to 15, mustKeepSerie is set to false but the loop is not impacted as you asked.

And in the while, we have a single condition to loop :

while ((input = in.nextInt()) >= 0 && counter < 20 ) 

which corresponds to :

it will only stop asking for more numbers once he reaches the maximum of 20 numbers or when he inputs a negative number.

 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int[] numbers = new int[20];

    boolean mustKeepSerie = true;
    int sum = 0, input = 0, counter = 0;

    System.out.print("Series ");
    while ((input = in.nextInt()) >= 0 && counter < 20) {
        numbers[counter] = input;
        sum += numbers[counter];

        // mustKeepSerie condition
        boolean hasbignumber = false;
        if (numbers[counter] > 15) {
          hasbignumber = true;
        }
        if (sum > 50 || hasbignumber) {
          mustKeepSerie = false;
        }
        counter++;
    }

    if (mustKeepSerie) {
       // do your processing     
    }

   }

Edit new solution after comment

public static void main(String[] args) {
  Scanner in = new Scanner(System.in);

  int[] numbers = new int[20];

  // boolean mustKeepSerie = true;
  int sum = 0, input = 0, counter = 0;

  System.out.print("Series ");

  boolean isAcceptableSerie = false;

  boolean isCurrentSerieValid = true;

  while (!isAcceptableSerie) {

    input = in.nextInt();

    // condition exit or loop again if input <0
    if (input < 0) {

      if (isCurrentSerieValid) {
         isAcceptableSerie = true;
       }

       else {
         System.out.println("serie discarded!");
         sum = 0;
         counter = 0;
         isCurrentSerieValid=true;
       }
       continue;
    }

    numbers[counter] = input;
    sum += numbers[counter];

    if (sum > 50 || numbers[counter] > 15) {
      isCurrentSerieValid = false;
    }

    // condition exit or loop again if counter==19
    if (counter == 19) {
      if (isCurrentSerieValid) {
          isAcceptableSerie = true;
      }
      else {
          System.out.println("serie discarded!");
          sum = 0;
          counter = 0;
          isCurrentSerieValid=true;
      }
      continue;
    }
    counter++;
  }
}