-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

8
  • Can't reproduce the described behaviour Commented Apr 22, 2022 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. Commented Apr 22, 2022 at 19:56
  • We are using MindTap(Web IDE with auto check features) Commented Apr 22, 2022 at 19:57
  • 1
    while(reference != 20) Commented Apr 22, 2022 at 20:06
  • 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? Commented Apr 22, 2022 at 20:23

1 Answer 1

0

Change the while loop condition.

 while(reference != 20){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.