0

I'm having trouble solving this homework problem. The problem wants me to create a program that reads user input of numbers and get the minimum and maximum values of those numbers.

Basically, the output should be as follows:

Enter number count: 10

Enter 10 numbers separated by space and press ENTER: 1 2 3 1 2 3 4 5 6 3

Min is 1 and has 2 occurrences

Max is 6 and has 1 occurrences

I was able to create methods to get the min and max. I don't know how to get the number of occurrences for the min and max with what I have. I also don't know how to get the scanner to read an input of integers on the same line.

import java.util.Scanner;

public class homework2
{
  public int min(int[] array)
  {
    int min = array[0];
    for (int i = 0; i < array.length; i++)
    {
      if (array[i] < min)
      {
        min = array[i];
      }
    }
    return min;
  }

  public int max(int[] array)
  {
    int max = 0;
    for (int i = 0; i < array.length; i++)
    {
      if (array[i] > max)
      {
        max = array[i];
      }
    }
    return max;
  }

  public static void main(String[] args)
  {
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the length of the array:");
    int length = s.nextInt();
    int[] myArray = new int[length];
    System.out.println("Enter the elements of the array:");

    for (int i = 0; i < length; i++)
    {
      myArray[i] = s.nextInt();
    }
  }
}
Sandeepa
  • 2,522
  • 3
  • 14
  • 34
  • 2
    Keep an extra variable 'counter'. Every time you set a new minimum or maximum you reset it to 1. Then add an extra if to see if you encounter it again to increase the counter. – Babyburger Oct 14 '19 at 18:02
  • For the scanner, separate your input by a delimiter. See https://stackoverflow.com/questions/28766377/how-do-i-use-a-delimiter-in-java-scanner – Babyburger Oct 14 '19 at 18:03
  • 1
    `nextInt()` should read the numbers separated by space just fine. What errors are you experiencing? – Johnny Mopp Oct 14 '19 at 18:08

6 Answers6

0

One way is to store counts of each one of the numbers you see using a HashMap. The number itself can be the key and the value can be the count that you are incrementing. You can also use a Math lib to output a min and max from the set of keys (numbers)

https://www.geeksforgeeks.org/java-math-min-method-examples/

https://www.geeksforgeeks.org/count-occurrences-elements-list-java/

Good luck!

0

Once you know what the maximum is, just count how many times that occurs in the array.

However, as @Babyburger points out, you don't need an array to compute either the maximum or how many times it occurs.

Scott Hunter
  • 44,196
  • 8
  • 51
  • 88
0

Here's a simple program to do what you need:

public static void main(String[] args) {
    int[] array = {1, 2, 3, 1, 2, 3, 4, 5, 6, 3}; // get your actual array
    int first = array[0];

    // initial values
    int min = first;
    int minOccurs = 1;
    int max = first;
    int maxOccurs = 1;

    for(int i = 1; i < array.length; i++) {
        int current = array[i];
        if(current == min) {
            minOccurs++;
        } else if (current < min) {
            min = current;
            minOccurs = 1;
        }

        if(current == max) {
            maxOccurs++;
        } else if (current > max) {
            max = current;
            maxOccurs = 1;
        }
    }

    System.out.println("Min is " + min + " and has " + minOccurs + " occurrences");
    System.out.println("Max is " + max + " and has " + maxOccurs + " occurrences");
    // prints: "Min is 1 and has 2 occurrences"
    // prints: "Max is 6 and has 1 occurrences"
}
xtratic
  • 4,136
  • 2
  • 10
  • 28
0

Here is one way to parse a line of space separated integer numbers into int array:

int[] array = Arrays
            .stream(scanner.nextLine().trim().split("\\s+"))
            .mapToInt(Integer::parseInt)
            .toArray();
MartinBG
  • 1,034
  • 8
  • 13
0

This should work for you. Let me know if you have questions:

import java.util.Scanner;

public class homework2 {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the length of the array:");
        int length = s.nextInt();
        System.out.println("Enter the elements of the array:");
        int[] numbers = new int[length];
        for (int i = 0; i < length; i++) {
            numbers[i] = Integer.parseInt(s.next());
        }
        // take first element as max and min
        int max = numbers[0];
        int min = numbers[0];
        // max and min exists so occurs at least once
        int maxOcc = 1;
        int minOcc = 1;
        // start from i = 1; because we assigned the first element before
        for (int i = 1; i < length; i++) {
            int number = numbers[i];
            if (number > max) {
                max = number;
                // start counting again
                maxOcc = 1;
            } else if (number == max) {
                maxOcc++;
            }
            if (number < min) {
                min = number;
                // start counting again
                minOcc = 1;
            } else if (number == min) {
                minOcc++;
            }
        }
        System.out.println("max: " + max);
        System.out.println("maxOcc: " + maxOcc);
        System.out.println("min: " + min);
        System.out.println("minOcc: " + minOcc);
    }
}
elbraulio
  • 1,004
  • 4
  • 14
0

Here is a solution to your problem. This takes a single string with numbers and spaces in-between, adds them to a dynamic array(ArrayList) and uses the min,max functions. Do not initialize min and max=0 because we have to pick an integer from the provided list to start our comparison and that has to be the first element [0].

Code

import java.util.ArrayList;
import java.util.Scanner;
public class homework2 { 
//Using static with functions because of the function calls in the Static 
//main function
//Find the maximum number
public static int max(ArrayList<Integer> array) {
 int max=array.get(0);
    for(int i=0; i<array.size(); i++ ){
      if(array.get(i)>max) 
        max = array.get(i); 
 }
   return max;
 }

//Calculate Maximum number's occurences
public static int maxOccur(int max,ArrayList<Integer> array){
  int maxOccur=0;
   for(int i=0; i<array.size(); i++ )
      if(max==array.get(i)) maxOccur++;
  return maxOccur;
 }

//Find the minimum number
public static int min(ArrayList<Integer> array) {
 int min=array.get(0);
    for(int i=0; i<array.size(); i++ ){
      if(array.get(i)<min) 
        min = array.get(i);
  }  
  return min;
  }

 //Calculate Minimum number's occurences
 public static int minOccur(int min,ArrayList<Integer> array){
  int minOccur=0;
  for(int i=0; i<array.size(); i++ )
      if(min==array.get(i)) minOccur++;
  return minOccur;
  }

 public static void main(String args[]) 
 { 
      int minNum,maxNum; 
      Scanner in = new Scanner(System.in);  
      System.out.print("Enter the numbers separated by a space: ");  
      String number = in.nextLine();

    //Separate the string by spaces in-between
     String[] separatedNums = number.split(" ");
     //Create a dynamic ArrayList to store any amount of integers
    ArrayList<Integer> arrayList = new ArrayList<Integer>();
    //Save the above string in the ArrayList after parsing into integer
     for (String a : separatedNums) 
        arrayList.add(Integer.parseInt(a));
    minNum=min(arrayList);
    maxNum=max(arrayList);
     //Output the results
    System.out.println("Minimum Number="+minNum+" has 
    "+minOccur(minNum,arrayList)+" occurances");
    System.out.println("Maximum Number="+maxNum+" has 
    "+maxOccur(maxNum,arrayList)+" occurances");
     //Close the scanner
      in.close();
  } 
  } 
Adnan Temur
  • 303
  • 1
  • 10