-1

We have a High School activity where we utilize ArrayList methods to create a mean and median program. However, I encounter a problem when I inputted 20 data as shown below. All answers and suggestions are appreciated.

THE PROBLEM:

Create a MeanMedian2 class so that the user can enter any number of values up to 20. If the list has an even number of values, the median is the numeric average of the values in the two middle positions. Allow the user to enter 9999 to quit entering numbers.

//https://www.softwaretestinghelp.com/java-arraylist-tutorial/
//https://stackoverflow.com/questions/16242733/sum-all-the-elements-java-arraylist
//https://stackoverflow.com/questions/16242733/sum-all-the-elements-java-arraylist
//https://stackoverflow.com/questions/22023053/how-do-i-print-a-single-item-on-a-array-list

import java.util.*;
class MeanMedian2{

   public static void main(String[] args){
      Scanner input = new Scanner(System.in);
      ArrayList<Integer> numbers = new ArrayList<>();

      int reference = 0; 
      int count = 1;
      int ender = 9999;
      
      while(true){
          System.out.println("Enter number " + count + ":" );
          int num = input.nextInt();
          if (num == ender){
              break;
          }else{
              numbers.add(num);
              reference += 1;
              count += 1;
          }
      }

      //Mean
      double sum = 0;
      for (int i = 0; i < numbers.size(); i++)
           sum += numbers.get(i);
      double mean = sum/numbers.size();
      
      //Median
      int median;
      Collections.sort(numbers);
      int length = numbers.size();
      int oddOrEven = length % 2;
      int Odd = (length + 1)/2;

      if (oddOrEven != 0 ){ 
          median = numbers.get(Odd - 1);
      }else{
          int a = numbers.get((length/2)-1);
          int b = numbers.get(length/2);
          median = (a+b)/2;

      }
      
      

      //Final
      System.out.print("You entered: ");
      for(int i = 0; i < numbers.size() - 1; i++){
          System.out.print(numbers.get(i) + ", ");
      }
      System.out.println(numbers.get(length-1));
      System.out.println("The mean is " + mean + " and the median is " + median );
      

   }
}

Instance 1:

/**
Enter number 1:
25
Enter number 2:
50
Enter number 3:
500
Enter number 4:
550
Enter number 5:
450
Enter number 6:
600
Enter number 7:
200
Enter number 8:
10
Enter number 9:
700
Enter number 10:
9999
You entered: 10, 25, 50, 200, 450, 500, 550, 600, 700
The mean is 342.77777777777777 and the median is 450**/

Instance 2. I inputted 20 data and 1 9999 to stop (21 in all)

/** 
javac MeanMedian2.java
java MeanMedian2Exception in thread "main" 
java.util.InputMismatchException
     at java.util.Scanner.throwFor(Scanner.java:864)
     at java.util.Scanner.next(Scanner.java:1485)
     at java.util.Scanner.nextInt(Scanner.java:2117)
     at java.util.Scanner.nextInt(Scanner.java:2076)
     at MeanMedian2.main(MeanMedian2.java:19)**/

enter image description here

R.E.F.
  • 37
  • 7
  • [Can't reproduce the described behaviour](https://ideone.com/2wVbO2) – JustAnotherDeveloper Apr 22 '22 at 19:52
  • 1
    Me neither, I copy and pasted your code. Ran it entering 20 numbers (1 thru 20) then 9999 and the code worked correctly. My assumption is that you must have entered a non-integer value. – hfontanez Apr 22 '22 at 19:56
  • We are using MindTap(Web IDE with auto check features) – R.E.F. Apr 22 '22 at 19:57
  • My assumption is correct. The last value you entered was not an integer. That's what an `InputMistatchException` means. I am not sure what the original `NoSuchElement` was about. – hfontanez Apr 22 '22 at 19:58
  • 1
    while(reference != 20) – R.E.F. Apr 22 '22 at 20:06
  • We just transitioned to Java and I find this language very case sensitive. Thank You sires – R.E.F. Apr 22 '22 at 20:07
  • 1
    So judging from the screenshot, the exception is not in your code, because you see it runs perfectly fine in the terminal on the right side. But the exception is on the left side which I assume is part of MindTap (which I'm not familiar with). Which simply means I guess that you failed to implement it according to specification. Because it is requested to only allow 20 inputs and you allow more. Hence the `while(reference != 20)` solves it. Am I correct? – Ivo Apr 22 '22 at 20:23
  • I agree with Ivo Beckers. The `NoSuchElement` is a bug with the platform you are using. It's a bit disturbing that the "Test Case" pane states that it displays the mean and median of 10 input values, the framework supports 20 inputs, but you are lead to believe it can handle an unbounded amount of inputs. – hfontanez Apr 22 '22 at 20:49

1 Answers1

0

Change the while loop condition.

 while(reference != 20){
R.E.F.
  • 37
  • 7