Best way to add an input enter part(scanf) into a java function returns array

3612 views java
0

I just want to add a simple where I can add an input line part by user instead of hard code defining an array. So I can just enter a couple number as a part of an array.

Here is my original code:

public class Test {
     public static void main(String[] args)  {
         System.out.println("Largest in given array is " + max());
        }
     static int array[] = {10, 324, 45, 90, 9808};

     // Method to find maximum in arr[]
     public static int max() {
         int i;
         // Initialize maximum element
         int max = array[0];

         // Traverse array elements from second and
         // compare every element with current max  
         for (i = 1; i < array.length; i++)
             if (array[i] > max)
                 max = array[i];
         return max;
     }
}

My logic is this, tell me if I'm right or not

I need to add import java.util.Scanner; and then enter Scanner input = new Scanner(System.in);

But next part confuses me, should I change the max() into a string?

import java.util.Scanner;
public class Test {
     public static void main(String[] args)  {
         System.out.println("Largest in given array is " + max(int[] array));
        }
     Scanner input = new Scanner(System.in);
     static int array[] = {10, 324, 45, 90, 9808};

     // Method to find maximum in arr[]
     public static int max(int[] array) {
         int x;
         // Initialize maximum element
         int max = array[0];

         // Traverse array elements from second and
         // compare every element with current max  
         for (x = 1; x < array.length; x++)
             if (array[x] > max)
                 max = array[x];
         return max;
     }
}

answered question

Have max() accept an Array and then pass the Array that the user inputs to max()

Try looking at this post for assigning user input to an Array: stackoverflow.com/questions/2795350/…

okay I will try it now, if I don't fix it can you post an answer?

For max() t accept aprray can you give me some guidance?

public static int max(int[] array)

I updated it now

1 Answer

10

I'd recommend a re-evaluation of your general design before adding user input. Using a global array variable is unnecessary and arbitrarily restricts the ability of the max function to perform work on anything other than the hard-coded global array. max should accept a parameter array to operate on; this enables reusability.

Your max function's logic seems accurate but will crash the program on empty input arrays.

When handling user input, you may want to be able to respond to variable sizes. For this, using an ArrayList is the easiest way to go.

I also recommend initializing loop counters such as i in loop scope.

Here's a possible rewrite on the way to dynamic arrays that you can use:

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int arr[] = new int[8];

        for (int i = 0; i < arr.length; i++) {
            System.out.print("Enter an integer: ");
            arr[i] = in.nextInt();
        }

        System.out.println("Largest in given array is " + max(arr));
    }

    public static int max(int arr[]) {
        if (arr.length == 0) {
            return -1;
        }

        int max = arr[0];

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }

        return max;
    }
}

Output:

Enter an integer:  1
Enter an integer:  2
Enter an integer:  3
Enter an integer:  6
Enter an integer:  5
Enter an integer:  3
Enter an integer:  4
Enter an integer:  1
Largest in given array is 6

And a repl to test with.

posted this

Have an answer?

JD

Please login first before posting an answer.