Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to find the array index of the lowest value in the array?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to find the array index of the lowest value in the array?

    import java.util.Scanner;
     
    class array2{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
     
    		int values;
    		int num[] = new int[10];
    		System.out.println("Enter 10 values: ");
    		for(int i=0;i<num.length;i++){		
    			num[i] = input.nextInt();
    			System.out.print(num[i]+" ");
    		}
    		int sum = 0;
    		for(int j : num){
    			sum += j;
    		}
    		System.out.print("\nThe average is "+sum/10);
     
    		int smallest = num[0];
            int largest = num[0];
     
            for(int i=1; i< num.length; i++){
                if(num[i] > largest){
                    largest = num[i];
    			}else if (num[i] < smallest){
                    smallest = num[i];    
    			}
            }
    		int indexs = smallest.indexOf(smallest);
    		System.out.println("\nindex of smallest number(s) is "+indexs);
     
    		System.out.println("\nSmallest Number is " + smallest);
     
     
    	}
    }
    I need to get the index position of the lowest values in the array, but the line "int indexs = smallest.indexOf(smallest);" gives me an error. How do you fix it?


  2. #2
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: How to find the array index of the lowest value in the array?

    There is no indexOf method of primitive int which is the dataType of smallest and hence you are getting the error. You need to loop through the entire array and find out the value which is lowest and its corresponding index.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,194
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: How to find the array index of the lowest value in the array?

    gives me an error.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to find 2nd largest array if array values like{10,20,92,81,92,34}
    By anjijava16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 04:59 PM
  2. Replies: 9
    Last Post: August 23rd, 2013, 10:52 AM
  3. help with index in array
    By lanya1 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 6th, 2013, 08:58 PM
  4. Finding the lowest number in an array.
    By EatMyBible in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 01:49 AM
  5. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM