0

I'm trying to randomly generate 5 numbers and print the largest one, it's okay to generate numbers but I get an error when trying to print out the largest number

My Code:

import java.util.Arrays;
import java.util.Random;

public class Project {

    public static void main(String[] args) {
        list(0, 0);
        printMax();}

    private static void list(int min, int max) {

        int[] numbers = new int[5];
        for (int i = 0; i<numbers.length; i++) {
            numbers[i] = (int)(Math.random() * 100 + 1);}
            
        System.out.println("Sayilar: " + Arrays.toString(numbers));
        return;}
        
    private static void printMax(int... numbers) {
    int result = numbers[0];

    for (int i = 1; i < numbers.length; i++) {
        if (numbers[i] > result) {
            result = numbers[i];}}
            
    System.out.println("\nThe largest value is " + result);
    return;}
}

the output I got:

Sayilar: [5, 48, 88, 28, 68]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Project.printMax(Project.java:20)
at Project.main(Project.java:8)
AomineDaici
  • 635
  • 1
  • 5
  • 13
  • 2
    You need to pass something to your `printMax` method. – Amongalen Dec 07 '20 at 08:28
  • When I send something to Printmax, I print out what I am transmitting sample; printmax(0); --> The largest value is 0 – AomineDaici Dec 07 '20 at 08:32
  • @AomineDaici but you don't pass anything to your `printMax` method at the moment. 1) Make your `list` method return the `numbers` and then 2) pass this array to `printMax`. – maloomeister Dec 07 '20 at 08:34
  • Change your method declaration of `printMax` to `private static void printMax(int[] numbers)` and your IDE will probably tell you what's wrong. Varargs (`int...`) will also accept empty input. See [Arbitrary Number of Arguments](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html#varargs). – maloomeister Dec 07 '20 at 08:37
  • Also, `random.ints(from, to).limit(5).toArray()` (using `Random`) to generate random numbers is a lot cleaner. – Glains Dec 07 '20 at 08:54

3 Answers3

4

You didn't pass any numbers into the parameters of the printMax() method.


Here's a fix:

public static void main(String[] args) {
            int[] numbers = list();
            System.out.println(Arrays.toString(numbers));
            printMax(numbers);
        }
    
        private static int[] list() {
            int[] numbers = new int[5];
            for (int i = 0; i<numbers.length; i++) {
                numbers[i] = (int)(Math.random() * 100 + 1);}
    
            return numbers;
        }
    
        private static void printMax(int[] numbers) {
            int result = numbers[0];
    
            for (int i = 1; i < numbers.length; i++) {
                if (numbers[i] > result) {
                    result = numbers[i];
                }
            }

        System.out.println("\nThe largest value is " + result);
    }

I've changed the list() function to return the array it produces and then passed it onto the printMax(int[]) function which receives an array of ints.

MrBorder
  • 359
  • 3
  • 8
  • Yes, this and the other answer shows two ways of getting a return value from a function and passing it as an argument to another. You can either assign a variable explicitly, like here, or run everything in one line where you pass the result of the first function into the other as argument without storing it in a variable. I do not know which method produces the most efficient bytecode, however. – Henning Odén Dec 07 '20 at 08:48
2

Your code need some small changes. Read the comments below

import java.util.Arrays;
import java.util.Random;

public class Project {

    public static void main(String[] args) {
        // list will return an array of integers and pass that array as argument to printMax
        printMax(list(0, 0));}

    private static int[] list(int min, int max) { 
        int[] numbers = new int[5];
        for (int i = 0; i<numbers.length; i++) {
            numbers[i] = (int)(Math.random() * 100 + 1);}
            
        System.out.println("Sayilar: " + Arrays.toString(numbers));
        // return the numbers array
        return numbers;}
    // printMax takes an array of integers and does its logic
    private static void printMax(int [] numbers) {
    int result = numbers[0];

    for (int i = 1; i < numbers.length; i++) {
        if (numbers[i] > result) {
            result = numbers[i];}}
            
    System.out.println("\nThe largest value is " + result);
    return;}
}
ABC
  • 595
  • 2
  • 5
  • 20
0

Altough this question already has an answer, here is a more concise way to generate the random numbers using an IntStream. The Random instance should be reused when possible.

Random random = new Random();
int[] ints = random.ints(0, 10).limit(5).toArray();

System.out.println("Random values: " + Arrays.toString(ints));

int max = 0;
for (int i : ints) {
    if (i > max) max = i;
}
    
System.out.println("Maximum: " + max);
Glains
  • 2,773
  • 3
  • 16
  • 30