So.. I have This program that generates Random Numbers with the User input.
Now I Don't know how to find the Maximum and Minimum Value from that Random Numbers.

This Code Generates Numbers So Far with No Errors.

This Is My Code So Far.

    package patel.Jainam;
    import java.util.Random;
    import java.util.Scanner;

    public class SelectionSort {   
    public static void main(String[] args) 
        int MaxInteger = 0;
        int MinInteger;
        int [] Random;

        Scanner in = new Scanner( System.in );    
        Random random = new Random();    
        System.out.println ("Enter a Number: ");
        int number = in.nextInt();  
        Random = new int[number];    
        for (int i = 0; i < number; i++) {       
          int randomInteger = random.nextInt();    
          randomInteger [i] = MaxInteger;
         System.out.println("Random Integer: " + randomInteger);
        }    
      }

}

The error is saying the follwing: The Type of the expression Must be in array type but Resolved in int

Please Helppp

I'd say your problem is here:

int randomInteger = random.nextInt();    
randomInteger [i] = MaxInteger;

You declare randomInteger as an int then in the line treat it as an array.
In anwser to your max and min question, once you have your random numbers, loop through them and compare them to the current highest and low variables (intially set to 0 for the maxInteger and some over the top value for the minInteger say 99999). If the random value is higher than the maxInteger, store it in maxInteger. And do the same for minInteger. After one loop through the array you have your highest and lowest values.

so I tried this.

and Still not Working

    package patel.Jainam;

    import java.util.Random;
    import java.util.Scanner;

    public class SelectionSort {    

    public static void main(String[] args) {

        int maxValue;
        int minValue;
        Random randomNumber = new Random();

        Scanner in = new Scanner( System.in );

        System.out.println ("Enter a Number: ");
        int number = in.nextInt();

        int numArray [] = new int [number]; 

        for (int i = 0; i < number; i++) {       
           int randomInteger = randomNumber.nextInt(); 
            maxValue = numArray[i];
            minValue = numArray[i];      

            System.out.println("Largest Number is : " + maxValue);
            System.out.println("Smallest Number is : " + minValue);

           if (numArray [i] > maxValue){
               maxValue = numArray [i];
           }
           else if (numArray [i] < minValue){
               minValue = numArray [i];
           }
           System.out.println("Random Integer: " + randomInteger);

        } 
      }

  }

Edited 9 Months Ago by J_2

why would you want to do this:

for (int i = 0; i < number; i++) {       
           int randomInteger = randomNumber.nextInt(); 
            maxValue = numArray[i];
            minValue = numArray[i];

This means you overwrite the maxValue and minValue each iteration, regardless whether the value is more than the existing max, or lower than the existing minimum.

at which point, you go on and do this:

if (numArray [i] > maxValue){
               maxValue = numArray [i];
           }
           else if (numArray [i] < minValue){
               minValue = numArray [i];
           }

neither of these statements will be executed, since for the current situation:
maxValue == numArray[i] is true, and minValue == numArray[i] is true

Your code is not all that bad, but your logic is very flawed. re-write your analysis in pseudo code, and follow the flow. only when you think that is correct, write your code, and check for yourself whether your code truly reflects your analysis, and behaves exactly the way you want it to do.

This article has been dead for over six months. Start a new discussion instead.